Using method -canvas.drawBitmap(bitmap, src, dst, paint)

后端 未结 3 719
灰色年华
灰色年华 2020-12-15 16:26

Everytime I use this code nothing is drawn. I need to draw a bitmap inside of a specified rectangle.

canvas.drawBitmap(MyBitmap, null, rectangle, null)
         


        
3条回答
  •  一个人的身影
    2020-12-15 17:05

    The main item to remember when defining the Rect is:

    • left < right and top < bottom

    The rect is in screen coordinates (positive Y downward) ...

    I find it helpful to think of the Rect arguments

    (left, top, right, bottom)
    

    as

    (X, Y, X + Width, Y + Height)
    

    where X,Y is the top left corner of the sprite image.

    NOTE: If want to center the image on a particular location, remember to offset those values by half the sprite width & height. For example:

    int halfWidth = Width/2;
    int halfHeight = Height/2
    Rect dstRectForRender = new Rect( X - halfWidth, Y - halfHeight, X + halfWidth, Y + halfHeight );
    canvas.drawBitmap ( someBitmap, null, dstRectForRender, null );
    

    This uses the whole original image (since src rect is null) and scales it to fit the size and position from dstRectForRender ... and using the default Paint.

提交回复
热议问题