ImageView Pinch-zoom Scale Limits and pan bounds

前端 未结 4 1158
心在旅途
心在旅途 2020-12-14 23:03

I wanted to create a gallery with images. The images within the gallery should be zoomable and pannable. I could able to pinch-zoom an image but could not able to set zoom l

4条回答
  •  温柔的废话
    2020-12-14 23:35

    Have tried using this code below and although it works I found that it was causing small incremental values to accumulate in Trans X and Trans Y leading to the picture slowly moving off screen.

    if(scaleX <= 0.7f)
                matrix.postScale((0.7f)/scaleX, (0.7f)/scaleY, mid.x, mid.y);
        else if(scaleX >= 2.5f) 
                matrix.postScale((2.5f)/scaleX, (2.5f)/scaleY, mid.x, mid.y);
    

    I fixed this be storing an original matrix in a float[] (1,0,0,0,1,0,0,0,1) and resetting the matrix back to these values whenever scaleX < 1. Hope this helps someone else one day :)

    if(scaleX <= 1.0f) {                                   
        float[] originalmatrixvalues = new float[] {1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f};
        matrix.setValues(originalmatrixvalues); 
    } else if(scaleX >= 2.5f) {
        matrix.postScale((2.5f)/scaleX, (2.5f)/scaleY, mid.x, mid.y);
    }
    

提交回复
热议问题