【问题标题】:Find new coordinates of overlay object on the canvas after modifying on matrix in Android在Android中修改矩阵后在画布上找到覆盖对象的新坐标
【发布时间】:2014-08-19 08:35:54
【问题描述】:

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

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

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

【问题讨论】:

    标签: android matrix view popupwindow overlays


    【解决方案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)
    

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

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多