我试图在创建最终位图之前在单个操作中进行缩放和旋转但是preRotate,postConcat似乎不起作用.
Bitmap bmp = ... original image ...
Matrix m = new Matrix()
m.setScale(x, y);
m.preRotate(degrees, (float) width / 2, (float) height / 2);
Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
Run Code Online (Sandbox Code Playgroud)
它只适用比例而不是旋转.
我一直在查看文档,并且唯一drawBitmap()允许Matrixas参数不通过参数采用任何类型的屏幕坐标.
我正在使用以下代码绘制矩阵旋转的位图:
Matrix matrix = new Matrix();
matrix.setRotate(rotation,bitmap.getWidth()/2,bitmap.getHeight()/2);
canvas.drawBitmap(bitmap, matrix, null);
Run Code Online (Sandbox Code Playgroud)
这当然是我的位图0,0.如何设置位置并使用矩阵?
理想情况下,我想使用目标Rect:
this.destRect = new Rect(
x - spriteWidth / 2,
y - spriteHeight / 2,
x + spriteWidth / 2,
y + spriteHeight /2
);
Run Code Online (Sandbox Code Playgroud)
编辑:
以下建议在评论中我试过这个:
Matrix matrix = new Matrix();
matrix.setTranslate(x, y);
matrix.setRotate(rotation,bitmap.getWidth()/2,bitmap.getHeight()/2);
canvas.drawBitmap(bitmap, matrix, null);
Run Code Online (Sandbox Code Playgroud)
但无济于事.仍然绘制了位图0,0
EDIT2:
以下是使用矩阵在自身上旋转图像然后将其放在以下位置的技巧x,y:
Matrix matrix = new Matrix();
matrix.reset();
matrix.postTranslate(-bitmap.getWidth() / 2, -bitmap.getHeight() / 2); // Centers image
matrix.postRotate(rotation); …Run Code Online (Sandbox Code Playgroud)