Android: Canvas drawBitmap?

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

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

      

Here's 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 draw

  • src May be null. Subset of the bitmap to draw

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

  • paint May be empty. The paint used to paint the bitmap

What's missing in my code? Thank!

+3


source to share


1 answer


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

So it should look like this:



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

      

+1


source







All Articles