Pei-turn
Pei-turn

Reputation: 90

Zoom Imageview using matrix

Hi I am trying to zoom in on a specific point on the image in my imageview with this code:

private void calculateAndZoom() {

    matrix.set(getImageMatrix());
    printMatrixValues(getImageMatrix());

    float startpointY = (start_y_rel/ template_y_rel) * getHeight() * scale;
    float startpointX = (start_x_rel / template_x_rel)* getWidth() * scale;

    PrintDevMessage.print("Width: " + getWidth() + " Height: " + getHeight());

    matrix.setScale(1,1,0,0);
    matrix.postScale(scale, scale, startpointX, startpointY);

    this.setScaleType(ImageView.ScaleType.MATRIX);
    this.setImageMatrix(matrix);

    printMatrixValues(getImageMatrix());
}

start_y_rel and start_x_rel is points relative to the template_y_rel and template_x_rel

I am using matrix.setScale(1,1,0,0) to remove any previous zooming and move to the first position.

This code works with scale 3 but when I try another scale it zooms in on the wrong position.

Upvotes: 1

Views: 959

Answers (1)

Pei-turn
Pei-turn

Reputation: 90

Ok so after 3 days of head-scratching i found the solution:

private void calculateAndZoom() {

    float cScale=getMatrixValue(getImageMatrix(),Matrix.MSCALE_X);

    float newScale = ((float)1.0/cScale)*scale;

    matrix.set(getImageMatrix());
    printMatrixValues(matrix);

    float startpointY = ((start_y_rel / template_y_rel) * getHeight());
    float startpointX = ((start_x_rel / template_x_rel) * getWidth());

    PrintDevMessage.print("Width: " + getWidth() + " Height: " + getHeight());

    matrix.postScale(newScale, newScale, startpointX, startpointY);

    this.setScaleType(ImageView.ScaleType.MATRIX);
    this.setImageMatrix(matrix);

    printMatrixValues(getImageMatrix());

}

I did not account for the image being larger than the screen and when placed in the imageview the scale is changed. So i had to recalculate the scale i wanted with the old scale like this:

    float newScale = ((float)1.0/cScale)*scale;     

Upvotes: 1

Related Questions