I have a code which zooms and pans the imageview matrix, it works well but i want the imageview to not be zoomed smaller than my screen, and i don't want it to be zoomed very much, i want to set a limit for zooming and to the same thing for dragging(panning) it should pan horizontally if image's width is larger than screen, and it should pan vertically if image's height is larger than the screen, how can i achieve this result ? i tried some methods from mike Ortiz's but i couldn't get them to work.
2 Answers
I coded this for my app, and it's tricky to get it all right.
I created some Rect
s and RectF
s to do a lot of the interim calculations right off the bat. It's more efficient when you don't have to allocate these on every operation.
I used Matrix.setRectToRect()
to find the minimum scale factor, and 3x that for the maximum. Then after the postScale
on zoom, I clamp the new [absolute] scale factor to min/max.
Also after the postScale
, I also compare the rect coordinates to the screen coordinates and add a translation to keep the image corners outside the screen boundaries. This same logic is also done for dragging operations.

- 30,387
- 5
- 62
- 74
-
thank you , i really got the basic idea on what should i do, and resolved it, thanks again ! – Blerim Blerii Sep 26 '15 at 13:21
look into this library
https://github.com/davemorrissey/subsampling-scale-image-view
Highly configurable, easily extendable view with pan and zoom gestures for displaying huge images without loss of detail. Perfect for photo galleries, maps, building plans etc.

- 1,446
- 1
- 17
- 30
-
thanks for support, but i should keep it my project as cleanly as possible without using libraries – Blerim Blerii Sep 26 '15 at 11:14
-
1There is one more option. You can look into the code that he wrote to handle different situations. – Ravi Gadipudi Sep 26 '15 at 11:16
-
1I wrote this code for my app, so I know exactly what you're up against. I'm working on an answer for you. – kris larson Sep 26 '15 at 11:47
-
@BlerimBlerii if you don't want to include the library, just copy the source into your app. It's only a few classes. However the library is only 46Kb so won't make your app much bigger. – Dave Morrissey Oct 01 '15 at 17:33
-
@DaveMorrissey thanks, i have resolved it already, thanks again for support – Blerim Blerii Oct 01 '15 at 17:41