geo*_*rge 6 android canvas bitmap rotation matrix
好吧也许我在这里遗漏了一些东西,但我被困了好几个小时.我创建了一个应用程序,用户在图片上绘制尺寸线.现在我想绘制一些选择点,表明该行已被选中.这些点是一个特定的位图,必须位于该行的末尾(箭头后)并根据箭头旋转.我创建了一个扩展View的类DrawSelectionPoint,我可以使用以下内容旋转位图:
selectionPoint = BitmapFactory.decodeResource(context.getResources(),
R.drawable.selectionpoint);
Matrix matrix = new Matrix();
matrix.postRotate((float)Math.toDegrees(angle));
canvas.drawBitmap(selectionPoint, matrix, null);
Run Code Online (Sandbox Code Playgroud)
(其中angle是直线的角度)这样我的位图按照我想要的方式旋转,但是它绘制在点0,0(屏幕的左上角).
如果我使用类似的东西
canvas.save();
canvas.rotate();
canvas.drawBitmap(selectionPoint, x, y, null);
canvas.restore();
Run Code Online (Sandbox Code Playgroud)
然后我发现在我想要的确切位置绘制位图太难了(因为我在旋转的画布上绘制,然后我将其旋转回来).我尝试了一些欧几里德旋转变换,但我没有运气.
有没有办法应用矩阵旋转,并给出我需要绘制位图的点?先感谢您!
yoa*_*oah 19
假设您要绘制位图中心位于(px,py)画布坐标的位图.有一个成员变量
Matrix matrix = new Matrix();
Run Code Online (Sandbox Code Playgroud)
并在你的onDraw:
matrix.reset();
matrix.postTranslate(-bitmap.getWidth() / 2, -bitmap.getHeight() / 2); // Centers image
matrix.postRotate(angle);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, null);
Run Code Online (Sandbox Code Playgroud)