Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable image on a canvas

How can I get an image to the canvas in order to draw on that image?

like image 361
Lana Avatar asked Mar 03 '11 03:03

Lana


People also ask

How do I create a Bitmap in canvas?

To create a Bitmap from a resource, use the BitmapFactory method decodeResource(): Bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.


2 Answers

The good way to draw a Drawable on a canvas is not decoding it yourself but leaving it to the system to do so:

Drawable d = getResources().getDrawable(R.drawable.foobar, null); d.setBounds(left, top, right, bottom); d.draw(canvas); 

This will work with all kinds of drawables, not only bitmaps. And it also means that you can re-use that same drawable again if only the size changes.

like image 67
Gábor Avatar answered Nov 15 '22 22:11

Gábor


You need to load your image as bitmap:

 Resources res = getResources();  Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.your_image); 

Then make the bitmap mutable and create a canvas over it:

Canvas canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true)); 

You then can draw on the canvas.

like image 44
Konstantin Burov Avatar answered Nov 15 '22 22:11

Konstantin Burov