Java (Android) inserts icons on the Canvas

Java (Android) inserts icons on the Canvas … here is a solution to the problem.

Java (Android) inserts icons on the Canvas

So I have custom views, which I draw on Canvas. I want to add an icon or some image on Canvas. I tried

canvas.setBackgroundResource(R.drawable.image_name)

The problem here is that my icons are resized to fit the screen and I have no control over resizing or scaling the image.

Maybe I can get the height and width of the canvas and then create a drawable/bitmap with an unscaled icon and fill the background with some color?
Can anyone point me in the right direction?

Solution

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

You can use icon.getIntrinsicHeight()

and icon.getIntrinsicWidth() to get the preferred size of the icon.

Related Problems and Solutions