0

我正在开发 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)与方程式合并?

或者还有另一种方法可以在修改矩阵后在画布上找到覆盖对象的新坐标?

请帮我解决这个问题。我将不胜感激。谢谢你。

4

1 回答 1

1

尝试使用 matrix.mapPoints()

你必须有图像的绝对坐标

如何绝对协调?

void calculaCoordenadasImagen(MotionEvent e){
    float []m = new float[9];
    matrix.getValues(m);
    float transX = m[Matrix.MTRANS_X] * -1;
    float transY = m[Matrix.MTRANS_Y] * -1;
    float scaleX = m[Matrix.MSCALE_X];
    float scaleY = m[Matrix.MSCALE_Y];
    lastTouchX = (int) ((e.getX() + transX) / scaleX);
    lastTouchY = (int) ((e.getY() + transY) / scaleY);
    lastTouchX = Math.abs(lastTouchX);
    lastTouchY = Math.abs(lastTouchY);
}

然后使用2个不同的数组来保存点

int [] absolute = new int[2];
absolute[0]=lastTouchX;
absolute[1]=lastTouchY;

int [] points = new int[2];
points = matrix.mapPoints(absolute)

在绝对中你有绝对坐标,在点中你有你想知道的点

我希望它有帮助!

于 2014-11-11T08:34:32.683 回答