2

首先让我说我已经阅读了大量的缩放问题并在这里重新计算坐标。但我似乎无法将它们应用于我的情况。

我有一个可以缩放和滚动的自定义 ImageView 类。我遇到的问题来自按此顺序执行以下操作:

  1. 放大一个点,并使用该点作为焦点(这有效)
  2. 在放大的同时滚动
  3. 尝试在不同的点上放大/缩小(不起作用

这是自定义视图的示例。有两个圆圈:红色和绿色。红色是焦点应该在的位置,绿色是焦点接近实际位置的位置。 在此处输入图像描述

当我尝试放大橙色圈出的区域时,就会出现问题。在下图中,我尝试放大橙色圆圈区域(不是很大,只是为了显示计算出焦点的位置)。正如您在下面看到的,红色圆圈正确计算了新焦点,但由于某种原因,绿色圆圈实际上是它放大/缩小的位置。

在此处输入图像描述

我正在使用以下代码将缩放坐标与实际的图像视图坐标映射

public boolean onScale(ScaleGestureDetector detector) {
        //update the current scale
    scaleFactor *= detector.getScaleFactor();
    scaleFactor = Math.max(ZOOM_LEVEL_4, Math.min(scaleFactor, ZOOM_LEVEL_0));

    //applying the scaleFactor to the focal point as determined in onScaleBegin
    transformMatrix.setScale(scaleFactor, scaleFactor, 
                  newFocalPoints[0], newFocalPoints[1]);

    //apply the matrix to the child
    child.transform(transformMatrix, newFocalPoints[0],  newFocalPoints[1],
                oldFocalPoints[0], oldFocalPoints[1]);
    return true;
}

@Override
public boolean onScaleBegin(ScaleGestureDetector detector){
    //when a new scaling process begins, get the current imageMatrix and 
    //map the points to account for the current zoom level

    //the initial points. based on screen location and current scroll pos
    float startX = detector.getFocusX() + getScrollX();
    float startY = detector.getFocusY() + getScrollY();
    oldFocalPoints = new float[]{startX, startY};

    //map oldFocalPoints to coordinates of the imageView based on current zoom
    Matrix inverseTransformMatrix = new Matrix();

    if(transformMatrix.invert(inverseTransformMatrix))
        inverseTransformMatrix.mapPoints(newFocalPoints, oldFocalPoints);

    return true;
}

上图中的绿点设置为{oldCoordinates[0], oldCoordinates[1]}用于调试目的,虽然它不是焦点,但它非常接近。所以看起来虽然我似乎正确地计算了新的焦点(红色圆圈),但它似乎并没有被正确应用。任何人都可以发现问题吗?提前致谢!

4

1 回答 1

4

经过一番痛苦,我找到了解决方案。这是代码:

@Override
public boolean onScale(ScaleGestureDetector detector) {

    scaleFactor *= detector.getScaleFactor();
    scaleFactor = Math.max(ZOOM_4, Math.min(scaleFactor, ZOOM_LEVEL_0));

    float xDiff = initialFocalPoints[0] - currentFocalPoints[0];
    float yDiff = initialFocalPoints[1] - currentFocalPoints[1];

    transformMatrix.setScale(scaleFactor, scaleFactor, 
                                 currentFocalPoints[0], currentFocalPoints[1]);
    transformMatrix.postTranslate(xDiff, yDiff);    
    child.setImageMatrix(transformMatrix);

    return true;
}

@Override
public boolean onScaleBegin(ScaleGestureDetector detector){

    float startX = detector.getFocusX() + getScrollX();
    float startY = detector.getFocusY() + getScrollY();

    initialFocalPoints = new float[]{startX, startY};

    if(transformMatrix.invert(inverseTransformMatrix))
    inverseTransformMatrix.mapPoints(currentFocalPoints, initialFocalPoints);
    return true;
}

产生差异的行如下:

float xDiff = initialFocalPoints[0] - currentFocalPoints[0];
float yDiff = initialFocalPoints[1] - currentFocalPoints[1];
transformMatrix.postTranslate(xDiff, yDiff);

答案很简单,只要找出两点之间的差异并在每次缩放图像时平移图像视图。

于 2013-09-03T21:58:39.087 回答