Amir_P
Amir_P

Reputation: 9019

Drawing drawable in canvas doesn't work

I want to draw a drawable on canvas with this code but it doesn't work and i don't know why

getResources().getDrawable(R.drawable.allergist).draw(canvas);

I set my custom views heigh and width to match parent but whole screen is white and there is no drawable on screen

Upvotes: 6

Views: 6925

Answers (2)

gilm
gilm

Reputation: 8050

We had a similar problem, and we solved it by calling drawable.invalidateSelf(). After that, it rendered successfully onto our Canvas.

Upvotes: 0

Jay Rathod
Jay Rathod

Reputation: 11245

You need to load your image as bitmap:

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

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

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

After that you can draw it on canvas.

EDIT 1

Set bounds To Drawable.

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

Upvotes: 6

Related Questions