如何在ImageView中缩放特定的XY坐标(点)

时间:2017-06-13 17:52:18

标签: android android-imageview android-canvas android-bitmap android-photoview

我已将此PhotoView库用于自定义ImageView。我想在特定点缩放图像。我找到的方法是setScale(float scale, float focalX, float focalY, boolean animate)

我想知道我能传递focalXfocalY的值,我有X和Y坐标,我正在传递它并且它会扩展到非常不同的位置。

这是一个片段,

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

2 个答案:

答案 0 :(得分:1)

将缩放设置为指定的比例。图像将以点为中心      (focusX,focusY)。这些浮点数范围从0到1,表示焦点      作为视图左侧和顶部的一部分。例如,左上角      图像的角落是(0,0)。右下角是(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);
}

希望这会有所帮助。快乐的编码。

答案 1 :(得分:1)

要在Imageview中缩放特定的XY坐标,您可以传递焦点和焦点的值以及缩放(必须介于最大缩放和PhotoView的最小刻度之间)和布尔值来设置动画。

获得max-min比例的代码:

mPhotoView.getMinimumScale();

mPhotoView.getMaximumScale();
焦点和焦点它可以是屏幕上的任何点,这里我有两个例子,一个是屏幕的中心,另一个是左上角。以下是两种情况的代码。

代码:

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);