Retain zoom in photoview without centre aligining

188 views Asked by At

I have a requirement with slight modification. This post retains the zoom on orientation change and making it centre. I don't want the image to be centre aligned. I just want the zoom to be maintained. How can this be achieved? Any help will be highly appreciated.

1

There are 1 answers

0
reactivedroid On BEST ANSWER

I achieved the desired behaviour after so much brain storming. Idea is to save the previous matrix and zoom scale of the image and then populate the previously stored matrix on the image. Fetching the previous values:

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

Now attach photoView.getViewTreeObserver().addOnPreDrawListener(this); Populate the above matrix when image is zoomed onPreDraw() method like this:

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

Hope this helps someone.