From df1fc6033a7997e49f4cf284b122641352c1e5ae Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 3 Dec 2012 12:12:08 +0300 Subject: add option to fit comics to width by default --- .../library/imagezoom/ImageViewTouchBase.java | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/it/sephiroth/android/library') diff --git a/src/it/sephiroth/android/library/imagezoom/ImageViewTouchBase.java b/src/it/sephiroth/android/library/imagezoom/ImageViewTouchBase.java index a0e1a3b..95d6333 100644 --- a/src/it/sephiroth/android/library/imagezoom/ImageViewTouchBase.java +++ b/src/it/sephiroth/android/library/imagezoom/ImageViewTouchBase.java @@ -39,6 +39,7 @@ public class ImageViewTouchBase extends ImageView implements IDisposable { protected final float[] mMatrixValues = new float[9]; protected int mThisWidth = -1, mThisHeight = -1; protected boolean mFitToScreen = false; + protected boolean mFitToWidth = false; final protected float MAX_ZOOM = 2.0f; protected RectF mBitmapRect = new RectF(); @@ -76,6 +77,13 @@ public class ImageViewTouchBase extends ImageView implements IDisposable { } } + public void setFitToWidth( boolean value ) { + if ( value != mFitToWidth ) { + mFitToWidth = value; + requestLayout(); + } + } + @Override protected void onLayout( boolean changed, int left, int top, int right, int bottom ) { super.onLayout( changed, left, top, right, bottom ); @@ -92,6 +100,9 @@ public class ImageViewTouchBase extends ImageView implements IDisposable { else getProperBaseMatrix( getDrawable(), mBaseMatrix ); setImageMatrix( getImageViewMatrix() ); + + if (mFitToWidth && getScale() - 1f <= 0.01) + zoomToWidth(); } } @@ -182,13 +193,17 @@ public class ImageViewTouchBase extends ImageView implements IDisposable { } protected void _setImageDrawable( final Drawable drawable, final boolean reset, final Matrix initial_matrix, final float maxZoom ) { - + if ( drawable != null ) { if ( mFitToScreen ) getProperBaseMatrix2( drawable, mBaseMatrix ); else getProperBaseMatrix( drawable, mBaseMatrix ); super.setImageDrawable( drawable ); + + if (mFitToWidth && getScale() - 1f <= 0.01) + zoomToWidth(); + } else { mBaseMatrix.reset(); super.setImageDrawable( null ); @@ -483,4 +498,17 @@ public class ImageViewTouchBase extends ImageView implements IDisposable { public void dispose() { clear(); } + + protected void zoomToWidth() { + RectF bitmapRect = getBitmapRect(); + + float w = bitmapRect.right - bitmapRect.left; + + float scale = 1f; + + if (w < getWidth()) { + scale = (float)getWidth() / w; + zoomTo(scale, 0f, 0f); + } + } } -- cgit v1.2.3