Skip to content Skip to sidebar Skip to footer

Scale Limits On Pinch Zoom Of Android

How to set Max and Min zoom levels for Pinch-Zoom? Here is my code: // public class TouchImageView extends ImageView { private static final String TAG = 'Touch'; /

Solution 1:

privatestaticfinalfloat MIN_ZOOM = 1.0f;
privatestaticfinalfloat MAX_ZOOM = 5.0f;

scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));

Solution 2:

Check my answer here. It worked with very little extra coding effort.

https://stackoverflow.com/a/18395969/2709830

Solution 3:

if(mapZoom > MAX_ZOOM && (newDist > oldDist) ) {
    break;
} elseif(mapZoom < MIN_Zoom && (newDist < oldDist) ){
    break;
}

matrix.postScale(zoomScale, zoomScale, mid.x, mid.y);  
savedMatrixZoom.set(matrix);  

It works fine but still I loose that smoothness and is too sensitive.

Solution 4:

Create a temporary matrix (temp), save the current matrix in it and scal the temp matrix. Then check the MSCALE_X value of temp matrix.

If zoom of your temp matrix is within your limit, postscale your matrix and save it in another matrix (savedMatrixZoom). If it is over your limit just load your current matrix from SavedMatrixZoom.

elseif (mode == ZOOM) {
        float newDist = spacing(event);
        Log.d(TAG, "newDist=" + newDist);
        if (newDist > 10f) {
            matrix.set(savedMatrix);
            zoomScale = newDist / oldDist;

            Matrix temp = new Matrix();
            temp.set(matrix);
            temp.postScale(zoomScale, zoomScale, mid.x, mid.y);
            mapZoom = getValue(temp, Matrix.MSCALE_X);
            if (mapZoom < MAX_ZOOM && mapZoom > MIN_ZOOM) {
                matrix.postScale(zoomScale, zoomScale, mid.x, mid.y);
                savedMatrixZoom.set(matrix);
            } else {
                matrix.set(savedMatrixZoom);
            }
        }
    }

Hope it helps

Post a Comment for "Scale Limits On Pinch Zoom Of Android"