diff options
author | David Morrissey <davemorrissey@gmail.com> | 2017-11-09 16:34:28 +0000 |
---|---|---|
committer | David Morrissey <davemorrissey@gmail.com> | 2017-11-09 16:34:28 +0000 |
commit | c87cdabad200fbdf1b28d0f4037348cbbd5c880e (patch) | |
tree | f29b0e8d5a010ef6a207edd6b4abebb5f032ada3 | |
parent | 13271e3a5461235523c854d73a1f2903f7bf995d (diff) | |
download | subsampling-scale-image-view-c87cdabad200fbdf1b28d0f4037348cbbd5c880e.tar.gz |
#331 Added methods to convert screen coordinates to file coordinates, to simplify extracting the visible area from an image.
-rw-r--r-- | library/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java index a77a46d..2993836 100644 --- a/library/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java @@ -1529,6 +1529,10 @@ public class SubsamplingScaleImageView extends View { int sHeight = dimensions.y; int exifOrientation = view.getExifOrientation(context, sourceUri); if (view.sRegion != null) { + view.sRegion.left = Math.max(0, view.sRegion.left); + view.sRegion.top = Math.max(0, view.sRegion.top); + view.sRegion.right = Math.min(sWidth, view.sRegion.right); + view.sRegion.bottom = Math.min(sHeight, view.sRegion.bottom); sWidth = view.sRegion.width(); sHeight = view.sRegion.height(); } @@ -2018,6 +2022,50 @@ public class SubsamplingScaleImageView extends View { } /** + * Converts a rectangle within the view to the corresponding rectangle from the source file, taking + * into account the current scale, translation, orientation and clipped region. This can be used + * to decode a bitmap from the source file. + * + * This method will only work when the image has fully initialised, after {@link #isReady()} returns + * true. It is not guaranteed to work with preloaded bitmaps. + * + * The result is written to the fRect argument. Re-use a single instance for efficiency. + */ + public void viewToFileRect(Rect vRect, Rect fRect) { + if (vTranslate == null || !readySent) { + return; + } + fRect.set( + (int)viewToSourceX(vRect.left), + (int)viewToSourceY(vRect.top), + (int)viewToSourceX(vRect.right), + (int)viewToSourceY(vRect.bottom)); + fileSRect(fRect, fRect); + fRect.set( + Math.max(0, fRect.left), + Math.max(0, fRect.top), + Math.min(sWidth, fRect.right), + Math.min(sHeight, fRect.bottom) + ); + if (sRegion != null) { + fRect.offset(sRegion.left, sRegion.top); + } + } + + /** + * Find the area of the source file that is currently visible on screen, taking into account the + * current scale, translation, orientation and clipped region. This is a convenience method; see + * {@link #viewToFileRect(Rect, Rect)}. + */ + public void visibleFileRect(Rect fRect) { + if (vTranslate == null || !readySent) { + return; + } + fRect.set(0, 0, getWidth(), getHeight()); + viewToFileRect(fRect, fRect); + } + + /** * Convert screen coordinate to source coordinate. */ public final PointF viewToSourceCoord(PointF vxy) { |