Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concepts about Android Drawable

Tags:

android

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.

like image 620
Heatork Avatar asked Dec 28 '22 22:12

Heatork


2 Answers

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
like image 61
SteD Avatar answered Dec 30 '22 12:12

SteD


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

like image 28
Mudassir Avatar answered Dec 30 '22 12:12

Mudassir