缩放ImageView时,相同的矩阵会导致不同的结果

时间:2013-09-10 22:29:49

标签: android android-imageview

缩放图像视图时,我允许图像在缩放变焦时变得小于最小尺寸,然后在onScaleEnd方法中恢复到最小尺寸。但是我遇到了一个问题:

构造函数中的语句transformMatrix = new Matrix()transformMatrix = child.getImageMatrix()之间没有区别 - 我已经检查过并且两个赋值都将返回矩阵:[1,0,0] [0,1,0 ] [0,0,1] ......但由于某种原因它们不能正常工作

以下是图像缩放的位置:

public boolean onScale(ScaleGestureDetector detector) {
        //update the current scale
        float oldScaleFactor = scaleFactor;
        scaleFactor *= detector.getScaleFactor();


        if(Math.abs(oldScaleFactor - scaleFactor) < 0.02 * scaleFactor){
            // don't update scale Factor if the event isn't being handled
            scaleFactor = oldScaleFactor; 
            return false; 
            // should prevent against changes from resting fingers in place
        }
        else{    
             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 void onScaleEnd(ScaleGestureDetector detector){
        //making sure the zoom level is within the allowable bounds
        if(scaleFactor < ZOOM_LEVEL_5 || scaleFactor > 8.0f){
        scaleFactor = Math.max(ZOOM_LEVEL_5, Math.min(scaleFactor, 8.0f));
        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);
    }
}

当我在构造函数中设置transformMatrix = new Matrix()时,一切正常,但这对transformMatrix = child.getImageMatrix()不起作用 - 图像剂量不会回弹到正确的最小尺寸......发生了什么?

帮助!

修改: 这是构造函数和onScaleBegin

public ScaleListener(){
        super();
        transformMatrix = new Matrix();
        transformMatrix = child.getImageMatrix();
        inverseTransformMatrix = new Matrix();
        currentFocalPoints = new float[2];
}

@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;
    }

1 个答案:

答案 0 :(得分:0)

我仍然没有确定为什么会发生这种情况,但我可以通过调用transformMatrix = new Matrix(child.getImageMatrix())来解决这个问题。这样它就包含了一个子imageView矩阵的副本,可以修改。