2

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

我想知道我可以传递什么值focalXand focalY,我有 X 和 Y 坐标,我目前正在传递它,它缩放到非常不同的位置。

这是一个片段,

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

2 回答 2

1

要在 Imageview 中缩放特定的 XY 坐标,您可以传递 focusX 和 focusY 的值以及比例(必须在 PhotoView 的最大比例和最小比例之间)和布尔值来设置动画。

获取最大最小比例的代码:

mPhotoView.getMinimumScale();

mPhotoView.getMaximumScale();

focusX 和 focusY 可以是屏幕上的任意点,这里我举了两个例子,一个是屏幕中心,另一个是左上角。以下是这两种情况的代码。

代码:

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);
于 2018-01-01T14:01:46.563 回答
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);
}

希望这可以帮助。快乐编码。

于 2018-01-01T09:11:03.187 回答