在没有居中对齐的情况下保留放大照片视图

时间:2016-12-21 06:57:41

标签: android image-processing android-photoview

我有一个需要稍作修改。此post保留缩放方向更改并使其居中。我不希望图像居中对齐。我只想保持缩放。怎么能实现这一目标?任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

经过如此多的脑力激荡,我达到了理想的行为。想法是保存图像的先前矩阵和缩放比例,然后在图像上填充先前存储的矩阵。 获取先前的值:

Matrix previousImageMatrix = photoView.getDisplayMatrix();
previousImageMatrix.getValues(previousValues);
previousZoomLevel = prescription.getScale();

现在附加photoView.getViewTreeObserver().addOnPreDrawListener(this);在缩放图像时填充上面的矩阵onPreDraw()这样的方法:

@Override
public boolean onPreDraw()
{
    photoView.getViewTreeObserver().removeOnPreDrawListener(this);
    Matrix theMatrix = photoView.getDisplayMatrix();
    Log.d("***", "onPreDraw: TransX = " + previousValues[2] + "TransY = " + previousValues[5]);
    float[] matrixValues = new float[9];
    matrixValues[0] = previousZoomLevel; // 0 and 4 store the scaleX and scaleY
    matrixValues[4] = previousZoomLevel;
    if (previousZoomLevel == MIN_ZOOM_LEVEL) // Zoom Level starts from 1.0 to 16.0
    {
        matrixValues[2] = 0; //2 and 5 store store TransX and TransY
        matrixValues[5] = 0;
    } else
    {
        matrixValues[2] = previousValues[2];
        matrixValues[5] = previousValues[5];
    }

    Log.d("***", "onPreDraw: NEW.. TransX = " + matrixValues[2] + "TransY = " + matrixValues[5]);
    matrixValues[8] = 1.0f;
    theMatrix.setValues(matrixValues);
    prescription.setDisplayMatrix(theMatrix);

    Matrix theImageViewMatrix = photoView.getDisplayMatrix();
    prescription.setImageMatrix(theImageViewMatrix);
    return true;
}

希望这有助于某人。