Search code examples
androidcanvasscalingrect

Android: Determine Coordinates of Viewable Area when Scaling


I have an app that allows the user to scale in for a closer view of a custom View using the ScaleGestureDetector.

What I would like to do is show the user a thumbnail of the larger screen when they are zoomed in. I'd like to show the thumbnail anchored to the bottom left corner of the visible area of the View, such that as the user scales in, the thumbnail stays visible.

My problem is that while I am able to determine when I should be showing the thumbnail in my View's onDraw() method, I can't figure out how to anchor the shape I wish to draw (a translucent Rect) in the visible corner. No problem drawing it to the canvas using absolute coordinates, but as soon as I scale in, it stays in the absolute corner.

I do track the current scaling factor as well as the scaling center (x,y), so I could likely do some calculations relating to the known window size, and these values to determine the corner, but just wondering if there are any routines or better ways to handle this.

Thanks for any info,

Paul


Solution

  • Figured out how to get the dimensions of the currently viewable area when scaling a canvas; calling canvas.getClipBounds(); in my onDraw gave me a Rect representing the 'clipped' canvas. From this I was able to access .top, .left, bottom .right and determine where to locate the thumbnail closeup view.