Java 在Android中绘制位图的部分?
我这里有一个在Android中绘制旋转缩放位图的方法:Java 在Android中绘制位图的部分?,java,android,graphics,Java,Android,Graphics,我这里有一个在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
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);
}
我不确定如何修改它以接受float sourceX、float sourceY、float sourceW、float sourceH
我想渲染平铺集,所以我需要告诉它在位图上显示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);
}
Android有一个drawBitmap(source rect,dest rect),所以它必须是可能的。你能旋转画布而不是旋转位图吗?然后你可以使用drawBitmap(source rect,dest rect)以旋转角度绘制位图的一部分。是的,我可以做canvas.rotate,但我不知道如何正确变换画布。