Search code examples
androidcanvaspositiontouchzooming

Getting a particular position in canvas after zoom the canvas android


I need to click on particular item on the canvas while zooming and moving functionalities also enable for canvas. I can calculate the rectangle position while moving the canvas. There I just calculate the touch movement distance by (CurrenTouchXPosition - StartXPosition).

public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();

    int x = (int) event.getX(); 
    int y = (int) event.getY();

    float moveOffsetX = (event.getX() - start.x);
    float moveOffsetY = (event.getY() - start.y);

Then,

switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            mode = DRAG;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_UP:
            Log.d(TAG, "action up");
                secondRectUpperX = secondRectUpperX + moveOffsetX;
                secondRectBottomX = secondRectBottomX + moveOffsetX;
                secondRectUpperY = secondRectUpperY + moveOffsetY;
                secondRectBottomY = secondRectBottomY + moveOffsetY;

This can identify the new canvas position of the rectangle. This works perfectly. I can identify the touch event of particular item while moving the canvas by this logic. But now i need to calculate the rectangle position relative to the canvas, after zoom the canvas. Whats the maths behind the zooming. If anyone knows please help in this. Thank you.


Solution

  • Finally I come up with a solution.

    I translate my touch position screen coordinates to canvas coordinates.

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                ...
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                ...
                break;
            case MotionEvent.ACTION_UP:
                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) ((event.getX() + transX) / scaleX);
                lastTouchY = (int) ((event.getY() + transY) / scaleY);
                lastTouchX = Math.abs(lastTouchX);
                lastTouchY = Math.abs(lastTouchY);
    

    Thanks for Andres Cardenas Pardo's answer here

    I could able to get the touch position coordinates according to the canvas coordinates. Since i know the coordinates of my drawn object, i check whether the touch position is within the range of drawn object.

    if((lastTouchX>=firstRectUpperX && firstRectBottomX>=lastTouchX) && (lastTouchY>=firstRectUpperY && firstRectBottomY>=lastTouchY)) {
        isbtn1Clicked = true;
    

    }