Heatork
Heatork

Reputation: 31

Concepts about Android Drawable

How to get a well understanding the concept of the classes of Canvas,Drawable,Bitmap and Paint? What's the relationship among them ?

Could someone please give me an example?

Thanks a lot.

Upvotes: 3

Views: 665

Answers (2)

Mudassir
Mudassir

Reputation: 13194

Android's Official documentation covers all the details about the API. And best way to learn something is learn by doing, so after reading the docs, google for tutorials and experiment, all your doubts will be cleared gradually.

Canvas

Drawable

Bitmap

Paint

Upvotes: 3

SteD
SteD

Reputation: 14025

From the Canvas class documentation:

The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

So you need 4 components in order to draw something.

  1. Bitmap
  2. Canvas
  3. Drawable
  4. Paint

Let's say you want to draw a circle on a background image from your drawable folder.

Canvas canvas;
Bitmap mBG;
Paint mPaint = new mPaint();

mBG = BitmapFactory.decodeResource(res, R.drawable.apple);  //Return a bitmap from the image from drawable folder
Canvas.drawBitmap(mBG, 0,0, null);     //Draw the bitmap

mPaint.setColor(Color.BLACK);          //Set paint to BLACK color
Canvas.drawCircle(100,100,30, mPaint); //Draw circle at coordinate x:100,y:100, radius 30 with the paint defined

Upvotes: 6

Related Questions