5
votes

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!

1
What's wrong with the result you got from this code?Kai
The result is a part of the image, but resized and not from the part I wantedEllS1
Is the code I wrote correct?EllS1

1 Answers

1
votes

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);