How can I get an image to the canvas in order to draw on that image?
To create a Bitmap from a resource, use the BitmapFactory method decodeResource(): Bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With