Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Canvas drawBitmap?

I have a 200x200px bitmap. I want to draw the top left 50x50px corner of my bitmap, on my canvas at coordinates 100,100 with a width and height of 50px, by using:

drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)

This is what I've tried:

drawBitmap(myBitmap, new Rect(0,0,50,50), new Rect(100,100,150,150) , null);

What am I doing wrong?


From developer.android.com:

Parameters

  • bitmap The bitmap to be drawn

  • src May be null. The subset of the bitmap to be drawn

  • dst The rectangle that the bitmap will be scaled/translated to fit into

  • paint May be null. The paint used to draw the bitmap

What is missing in my code? Thanks!

like image 445
EllS1 Avatar asked Nov 10 '22 00:11

EllS1


1 Answers

You need to change your rectangles. This is because, as described in the documentation, the first rectangle is the subset of the bitmap you want to draw, the second is the scaling/translating so basically the size of the destination draw (50x50)

So it should look like this:

drawBitmap(myBitmap, new Rect(100,100,150,150), new Rect(0,0,50,50) , null);
like image 194
fr4gus Avatar answered Nov 14 '22 22:11

fr4gus