Search code examples
androidmatrixviewpopupwindowoverlays

Find new coordinates of overlay object on the canvas after modifying on matrix in Android


I'm working on Android project and I'm using View class to drawing image and overlays. I'm using this code to drawing image and circle (it is working fine):

    @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 );
    }

Now I want to show PopupWindow windows as a tip on the image and on the same position of circle (testPoint). but the PopupWindows is not drawable object, so how can I find the new coordinates of testPoint after scaling, rotation and translating to put the coordinates of the PopupWindow same.

I'm trying to write code and it working fine, but for rotation and translating only without scaling, this is the code I wrote:

    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);

Where rc is the offset difference between bitmap view coordinates and the screen coordinates.

And I'm tested RotatePoint function and it working correctly.

Note: when the scale is equal 1 (disable scaling) the position of popup window is updating correctly if I rotate or move the image.

How can I merged the scale (if not equal 1 only) with equations?

Or there is another way to find new coordinates of overlay object on the canvas after modifying on matrix?

Please help me to solved this problem. I will be grateful for the help. Thank you.


Solution

  • try to use matrix.mapPoints()

    u must have the absolute coordinates of the image

    how to absolute coord?

    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);
    }
    

    then use 2 different arrays to save the points

    int [] absolute = new int[2];
    absolute[0]=lastTouchX;
    absolute[1]=lastTouchY;
    
    int [] points = new int[2];
    points = matrix.mapPoints(absolute)
    

    in absolute u have the absolute coordinates and in points u have the points u want know

    i wish it help!