How to scale specific XY coordinate (points) in ImageView

I used this PhotoView library for a custom ImageView. I want to scale an image at a specific location. Here's my method:setScale(float scale, float focalX, float focalY, boolean animate)

I am wondering that I can pass a value focalX

and focalY

, I have an X and Y coordinate that I am currently passing, and it scales to very different positions.

Here is a snippet,

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

      

+3


source to share


2 answers


To zoom in on a specific XY coordinate in Imageview, you can pass the focalX and focal area value along with the scale (must be between the PhotoView's max minscale scale) and a boolean value to set the animation.

Code for getting max-min scales:

mPhotoView.getMinimumScale();

mPhotoView.getMaximumScale();

      



focalX and focalY These can be any points on the screen, here I took two examples: one is from the center of the screen, and the other is the upper left corner. below 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);

      

+1


source


Set the scale to the indicated scale. The image will be centered around point (focusX, focusY). These floats range from 0 to 1 and designate the focal point as the left and top portion. For example, the top left corner of the image would be (0, 0). And the bottom right corner will be (1, 1).

public void setZoom(float scale, float focusX, float focusY, ScaleType scaleType) {

   /*setZoom can be called before the image is on the screen, but at this point,
   image and view sizes have not yet been calculated in onMeasure. Thus, we should
   delay calling setZoom until the view has been measured.*/

  if (!onDrawReady) {
     delayedZoomVariables = new ZoomVariables(scale, focusX, focusY, scaleType);
     return;
  }

  if (scaleType != mScaleType) {
     setScaleType(scaleType);
  }
  resetZoom();
  scaleImage(scale, viewWidth / 2, viewHeight / 2, true);
  matrix.getValues(m);
  m[Matrix.MTRANS_X] = -((focusX * getImageWidth()) - (viewWidth * 0.5f));
  m[Matrix.MTRANS_Y] = -((focusY * getImageHeight()) - (viewHeight * 0.5f));
  matrix.setValues(m);
  fixTrans();
  setImageMatrix(matrix);
}

      



Hope this helps. Happy coding.

+1


source







All Articles