我正在开发 Android 项目,我正在使用 View 类来绘制图像和叠加层。我正在使用此代码来绘制图像和圆圈(它工作正常):
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (!isInitialized) {
w = getWidth();
h = getHeight();
position.set(w / 2, h / 2);
// Scaling image to be fit screen
if(width<=height) { scale=((float)w)/width; } else { scale=((float)h)/height; }
isInitialized = true;
}
Paint paint = new Paint();
matrix.reset();
matrix.postTranslate(-width / 2.0f, -height / 2.0f);
matrix.postRotate((float)(angle*180/Math.PI));
matrix.postScale(scale, scale);
matrix.postTranslate(position.getX(), position.getY());
canvas.drawBitmap(bitmap, matrix, paint);
canvas.concat(matrix);
canvas.drawCircle(testPoint.x, testPoint.y, 20, paint );
}
现在我想将 PopupWindow 窗口显示为图像上的提示以及圆的相同位置(testPoint)。但是 PopupWindows 不是可绘制对象,所以如何在缩放、旋转和平移后找到 testPoint 的新坐标以使 PopupWindow 的坐标相同。
我正在尝试编写代码并且它工作正常,但是对于旋转和平移仅没有缩放,这是我写的代码:
int offsetX=(int) (-(width / 2.0f)+position.getX())+rc.left;
int offsetY=(int) (-(height / 2.0f)+position.getY())+rc.top;
Point RotatedPoint = RotatePoint(testPoint, centerPoint, angle);
RotatedPoint.x +=offsetX;
RotatedPoint.y +=offsetY;
popup.update(RotatedPoint.x, RotatedPoint.y, -1, -1);
其中 rc 是位图视图坐标和屏幕坐标之间的偏移差。
我测试了 RotatePoint 功能,它工作正常。
注意:当比例等于 1(禁用缩放)时,如果我旋转或移动图像,弹出窗口的位置会正确更新。
如何将比例(如果不等于 1)与方程式合并?
或者还有另一种方法可以在修改矩阵后在画布上找到覆盖对象的新坐标?
请帮我解决这个问题。我将不胜感激。谢谢你。