androidandroid-imageviewandroid-canvasandroid-bitmapandroid-photoview

How to zoom at particular XY coordinate(points) in ImageView


I have used this PhotoView library for custom ImageView. I want to scale the image at particular point. Here is the method I found is setScale(float scale, float focalX, float focalY, boolean animate)

I am wondering what can I pass a value of focalX and focalY , I have X and Y coordinate which I am passing currently and it scales to very different position.

Here is a snippet,

intResultX = intTotalX / intArraySize;
intResultY = intTotalY / intArraySize;
mMap.setScale(5, intResultX, intResultY, true);

Solution

  • To zoom at particular XY coordinate in Imageview you can pass a value of focalX and focalY along with scale (must be between max scale an min scale of PhotoView) and boolean value to set animation.

    Code to get max-min scales:

    mPhotoView.getMinimumScale();
    
    mPhotoView.getMaximumScale();
    

    focalX and focalY It can be any points on screen, here I have taken two examples one is center of the screen and other is top-left corner. following is the code for both cases.

    Code:

    Random r = new Random();
    float minScale = mPhotoView.getMinimumScale();
    float maxScale = mPhotoView.getMaximumScale();
    float randomScale = minScale + (r.nextFloat() * (maxScale - minScale));
    
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;
    int centerX=width/2;
    int centerY =height/2;
    
    /*pass a value of focalX and focalY to scale image to center*/
    //mPhotoView.setScale(randomScale, centerX, centerY, true);
    
    /*pass a value of focalX and focalY to scale image to top left corner*/
    mPhotoView.setScale(randomScale, 0, 0, true);