我这里有一个在Android中绘制旋转缩放位图的方法:
public void drawRotatedScaledBitmap(Bitmap b,
float centerX, float centerY, float width, float height, float angle)
{
float scaleX = width / b.getWidth();
float scaleY = height / b.getHeight();
centerX -= (b.getWidth() * scaleX) / 2.0f;
centerY -= (b.getHeight() * scaleY) / 2.0f;
matrix.reset();
matrix.setTranslate(centerX, centerY);
matrix.postRotate(angle * (180.0f / (float)(Math.PI)),
centerX + (b.getWidth() * scaleX) / 2.0f,
centerY + (b.getHeight() * scaleY) / 2.0f);
matrix.preScale(scaleX,scaleY);
canvas.drawBitmap(b, matrix, null);
}
我不确定如何修改它以接收
浮点源 X、浮点源 Y、浮点源 W、浮点源 H。我想渲染图块集,所以我需要在位图上告诉它 64,64,64,64。
我该怎么做?
谢谢
也许你可以尝试 canvas.clipRect() 来指定绘图区域。
这是我如何解决它的方法:
public void drawRotatedScaledBitmap(Bitmap b,
float centerX, float centerY, float width, float height,
float sourceX, float sourceY,
float sourceWidth, float sourceHeight, float angle)
{
float cx = centerX;
float cy = centerY;
dstRect.left = (int)centerX - (int)(width / 2);
dstRect.top = (int)centerY - (int)(height / 2);
dstRect.right = dstRect.left + (int)width;
dstRect.bottom = dstRect.top + (int)height;
srcRect.left = (int)sourceX;
srcRect.top = (int)sourceY;
srcRect.right = srcRect.left + (int)sourceWidth;
srcRect.bottom = srcRect.top + (int)sourceHeight;
canvas.rotate(angle * (180.0f / (float)(Math.PI)),cx,cy);
canvas.drawBitmap(b, srcRect, dstRect, null);
canvas.rotate(-angle * (180.0f / (float)(Math.PI)),cx,cy);
}