Skip to content Skip to sidebar Skip to footer

Touch Screen Is Not Adjusting According To Zoomed Image

I am doing image annotations in android and I've added zooming functionality into it. So now when I am zooming the image and after that doing any annotation, its not on that point

Solution 1:

You can try below onDraw and onTouch Method.

onDraw

@OverrideprotectedvoidonDraw(Canvas canvas) {
    onDrawReady = true;
    imageRenderedAtLeastOnce = true;
    if (delayedZoomVariables != null) {
        setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX, delayedZoomVariables.focusY, delayedZoomVariables.scaleType);
        delayedZoomVariables = null;
    }
    super.onDraw(canvas);
}

onTouch

@Override
    public boolean onTouch(View v, MotionEvent event) {
        mScaleDetector.onTouchEvent(event);
        mGestureDetector.onTouchEvent(event);
        PointF curr = new PointF(event.getX(), event.getY());

        if (state == State.NONE || state == State.DRAG || state == State.FLING) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    last.set(curr);
                    if (fling != null)
                        fling.cancelFling();
                    setState(State.DRAG);
                    break;

                case MotionEvent.ACTION_MOVE:
                    if (state == State.DRAG) {
                        float deltaX = curr.x - last.x;
                        float deltaY = curr.y - last.y;
                        float fixTransX = getFixDragTrans(deltaX, viewWidth, getImageWidth());
                        float fixTransY = getFixDragTrans(deltaY, viewHeight, getImageHeight());
                        matrix.postTranslate(fixTransX, fixTransY);
                        fixTrans();
                        last.set(curr.x, curr.y);
                    }
                    break;

                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                    setState(State.NONE);
                    break;
            }
        }

        setImageMatrix(matrix);

        //// User-defined OnTouchListener//if(userTouchListener != null) {
            userTouchListener.onTouch(v, event);
        }

        //// OnTouchImageViewListener is set: TouchImageView dragged by user.//if (touchImageViewListener != null) {
            touchImageViewListener.onMove();
        }

        //// indicate event was handled//returntrue;
    }
}

Solution 2:

I finally found an answer to the problem I asked. It was happening because of the pixel mapping difference between the Touch Coordinates and the Canvas Coordinates.

Adding this to the onTouch Method Solved my problem

PointF curr = new PointF();
//Add this line
curr = transformCoordTouchToBitmap(event.getX(), event.getY(), false);
mX = curr.x;
mY = curr.y;

And define this method in your code.

private PointF transformCoordTouchToBitmap(float x, float y, boolean clipToBitmap) {
    matrix.getValues(m);
    floatorigW= getDrawable().getIntrinsicWidth();
    floatorigH= getDrawable().getIntrinsicHeight();
    floattransX= m[Matrix.MTRANS_X];
    floattransY= m[Matrix.MTRANS_Y];
    floatfinalX= ((x - transX) * origW) / getImageWidth();
    floatfinalY= ((y - transY) * origH) / getImageHeight();

    if (clipToBitmap) {
        finalX = Math.min(Math.max(finalX, 0), origW);
        finalY = Math.min(Math.max(finalY, 0), origH);
    }

    returnnewPointF(finalX, finalY);
}

Post a Comment for "Touch Screen Is Not Adjusting According To Zoomed Image"