我需要在指定的矩形内绘制位图。为什么没有绘制出来?
canvas.drawBitmap(MyBitmap, null, rectangle, null)
canvas.drawBitmap(MyBitmap, null, rectangle, null)
编辑
原回答不正确。 你可以使用sourceRect来指定要绘制的位图的一部分。 如果为null,则将使用整个图像。
根据fryer的评论,他是在某个物体下面绘制的,我会加上说明。
drawBitmap(bitmap, srcRect, destRect, paint)
不处理Z 排序(深度)
,调用对象的顺序很重要。
如果您要绘制3个形状,正方形、三角形和圆形,如果要使正方形处于最上层,则必须最后绘制它。
您没有指定任何源,因此未绘制任何内容。
示例:
您有一个100x100像素的位图。您想要绘制整个位图。
canvas.drawBitmap(MyBitmap, new Rect(0,0,100,100), rectangle, null);
canvas.drawBitmap(MyBitmap, new Rect(0,0,50,100), rectangle, null);
您需要指定源矩形,源矩形可以是从位图的0,0到宽度、高度的任何位置的矩形。
定义Rect时需要记住的主要内容是:
矩形在屏幕坐标系中(正Y向下)...
我觉得把Rect参数当做...
(left, top, right, bottom)
如何使用Python获取当前日期和时间?
(X, Y, X + Width, Y + Height)
X,Y是精灵图像左上角的位置。
注意:如果想要将图像居中于特定位置,请记得将这些值偏移半个精灵图像的宽度和高度。例如:
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 );
这将使用整个原始图像(因为源矩形为null),并将其缩放以适应dstRectForRender的大小和位置...并使用默认的Paint。
这对我有用!
Rect rectangle = new Rect(0,0,100,100);
canvas.drawBitmap(bitmap, null, rectangle, null);