Reputation: 31
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
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.
Upvotes: 3
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.
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