Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java (Android) Insert icon on canvas

So i have custom View, where i draw on canvas. I would like to add icon or some image on canvas. I tried with

canvas.setBackgroundResource(R.drawable.image_name)

The probleme here is that my icon is resized to fit the screen and i have no control to resize or scale image.

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

like image 849
Phil Boris Avatar asked Dec 15 '22 14:12

Phil Boris


1 Answers

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.

like image 180
pdegand59 Avatar answered Dec 17 '22 04:12

pdegand59