diff options
author | Mark Wei <markwei@google.com> | 2013-11-21 00:58:33 -0800 |
---|---|---|
committer | Mark Wei <markwei@google.com> | 2014-01-22 21:30:34 -0800 |
commit | 89e59f00d67791754e44e65413baa95f94056df4 (patch) | |
tree | 25c052346e9f118ed410eeffa58db0f104bbaf3b | |
parent | 1ef4c6b7fdbc6e36c030e4a6fab3a8ccf8ba524b (diff) | |
download | bitmap-89e59f00d67791754e44e65413baa95f94056df4.tar.gz |
Decouple parallaxSpeedMultiplier from setDecodeDimensions().
Expose decode vertical center through ExtendedOptions.
Genericize BitmapDrawableImageView#getTypedDrawable().
Bug: 11691744
Change-Id: I62e05ab2136cf031eaf1752dba728156c97c87a6
5 files changed, 70 insertions, 33 deletions
diff --git a/sample/src/com/example/bitmapsample/BitmapView.java b/sample/src/com/example/bitmapsample/BitmapView.java index 0613168..dc676dd 100644 --- a/sample/src/com/example/bitmapsample/BitmapView.java +++ b/sample/src/com/example/bitmapsample/BitmapView.java @@ -21,7 +21,6 @@ import android.graphics.Canvas; import android.util.AttributeSet; import android.widget.ListView; -import com.android.bitmap.drawable.BasicBitmapDrawable; import com.android.bitmap.drawable.ExtendedBitmapDrawable; import com.android.bitmap.view.BitmapDrawableImageView; @@ -29,7 +28,6 @@ public class BitmapView extends BitmapDrawableImageView { private final float mDensity; private ListView mListView; - private float mParallaxSpeedMultiplier; public BitmapView(Context c) { this(c, null); @@ -47,8 +45,8 @@ public class BitmapView extends BitmapDrawableImageView { @Override protected void onSizeChanged(final int w, final int h, int oldw, int oldh) { - ExtendedBitmapDrawable drawable = (ExtendedBitmapDrawable) getBasicBitmapDrawable(); - drawable.setDecodeDimensions(w, (int) (h * mParallaxSpeedMultiplier)); + ExtendedBitmapDrawable drawable = getTypedDrawable(); + drawable.setDecodeDimensions(w, h); } public void setListView(final ListView listView) { @@ -57,14 +55,10 @@ public class BitmapView extends BitmapDrawableImageView { @Override protected void onDraw(final Canvas canvas) { - ExtendedBitmapDrawable drawable = (ExtendedBitmapDrawable) getBasicBitmapDrawable(); + ExtendedBitmapDrawable drawable = getTypedDrawable(); float fraction = (float) getBottom() / (mListView.getHeight() + getHeight()); drawable.setParallaxFraction(fraction); super.onDraw(canvas); } - - public void setParallaxSpeedMultiplier(final float parallaxSpeedMultiplier) { - mParallaxSpeedMultiplier = parallaxSpeedMultiplier; - } }
\ No newline at end of file diff --git a/sample/src/com/example/bitmapsample/MainActivity.java b/sample/src/com/example/bitmapsample/MainActivity.java index cd8f6ac..5a2ac3a 100644 --- a/sample/src/com/example/bitmapsample/MainActivity.java +++ b/sample/src/com/example/bitmapsample/MainActivity.java @@ -118,11 +118,10 @@ public class MainActivity extends Activity { final ExtendedBitmapDrawable drawable = new ExtendedBitmapDrawable(getResources(), mCache, true /* limit density */, opts); - v.setBasicBitmapDrawable(drawable); + v.setTypedDrawable(drawable); v.setListView(mListView); - v.setParallaxSpeedMultiplier(NORMAL_PARALLAX_MULTIPLIER); } - v.getBasicBitmapDrawable().bind(new BitmapRequestKeyImpl(mItems[position])); + v.getTypedDrawable().bind(new BitmapRequestKeyImpl(mItems[position])); return v; } } diff --git a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java index 684d322..a09daad 100644 --- a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java +++ b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java @@ -119,10 +119,12 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, * * All UI operations should be called from the UI thread. */ - public final void setDecodeDimensions(int w, int h) { - mDecodeWidth = w; - mDecodeHeight = h; - bind(mCurrKey); + public void setDecodeDimensions(int width, int height) { + if (mDecodeWidth == 0 || mDecodeHeight == 0) { + mDecodeWidth = width; + mDecodeHeight = height; + setImage(mCurrKey); + } } /** @@ -133,6 +135,9 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, */ public void bind(RequestKey key) { Trace.beginSection("bind"); + if (mCurrKey != null && mCurrKey.equals(key)) { + return; + } setImage(key); Trace.endSection(); } @@ -153,10 +158,6 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, * Should only be overriden, not called. */ protected void setImage(final RequestKey key) { - if (mCurrKey != null && mCurrKey.equals(key)) { - return; - } - Trace.beginSection("set image"); Trace.beginSection("release reference"); if (mBitmap != null) { @@ -282,6 +283,10 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, return NO_MULTIPLIER; } + /** + * Clients can override this to specify which section of the source image to decode from. + * Possible applications include using face detection to always decode around facial features. + */ protected float getDecodeVerticalCenter() { return VERTICAL_CENTER; } diff --git a/src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java b/src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java index 6d9f05f..b6093e7 100644 --- a/src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java +++ b/src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java @@ -61,7 +61,6 @@ public class ExtendedBitmapDrawable extends BasicBitmapDrawable implements private final ExtendedOptions mOpts; // Parallax. - private static final float DECODE_VERTICAL_CENTER = 1f / 3; private float mParallaxFraction = 1f / 2; // State changes. @@ -157,12 +156,26 @@ public class ExtendedBitmapDrawable extends BasicBitmapDrawable implements setLoadState(LOAD_STATE_FAILED); } + /** + * Directly sets the decode width and height. The given height should already have had the + * parallaxSpeedMultiplier applied to it. + */ + public void setExactDecodeDimensions(int width, int height) { + super.setDecodeDimensions(width, height); + } + + /** + * {@inheritDoc} + * + * The given height should not have had the parallaxSpeedMultiplier applied to it. + */ @Override - protected void setImage(final RequestKey key) { - if (mCurrKey != null && mCurrKey.equals(key)) { - return; - } + public void setDecodeDimensions(int width, int height) { + super.setDecodeDimensions(width, (int) (height * mOpts.parallaxSpeedMultiplier)); + } + @Override + protected void setImage(final RequestKey key) { if (mCurrKey != null && getDecodeAggregator() != null) { getDecodeAggregator().forget(mCurrKey); } @@ -212,13 +225,13 @@ public class ExtendedBitmapDrawable extends BasicBitmapDrawable implements } @Override - protected float getDrawVerticalOffsetMultiplier() { + protected final float getDrawVerticalOffsetMultiplier() { return mOpts.parallaxSpeedMultiplier; } @Override protected float getDecodeVerticalCenter() { - return DECODE_VERTICAL_CENTER; + return mOpts.decodeVerticalCenter; } private DecodeAggregator getDecodeAggregator() { @@ -651,6 +664,23 @@ public class ExtendedBitmapDrawable extends BasicBitmapDrawable implements public final int features; /** + * Optional field for general decoding. + * + * This field determines which section of the source image to decode from. A value of 0 + * indicates a preference for the very top of the source, while a value of 1 indicates a + * preference for the very bottom of the source. A value of .5 will result in the center + * of the source being decoded. + * + * This should not be confused with {@link #setParallaxFraction(float)}. This field + * determines the general section for decode. The parallax fraction then determines the + * slice from within that section for display. + * + * The default value of 1f / 3 provides a good heuristic for the subject's face in a + * portrait photo. + */ + public float decodeVerticalCenter = 1f / 3; + + /** * Required field if {@link #FEATURE_ORDERED_DISPLAY} is supported. */ public DecodeAggregator decodeAggregator = null; @@ -725,6 +755,10 @@ public class ExtendedBitmapDrawable extends BasicBitmapDrawable implements */ private void validate() throws IllegalStateException { + if (decodeVerticalCenter < 0 || decodeVerticalCenter > 1) { + throw new IllegalStateException( + "ExtendedOptions: decodeVerticalCenter must be within 0 and 1, inclusive"); + } if ((features & FEATURE_ORDERED_DISPLAY) != 0 && decodeAggregator == null) { throw new IllegalStateException( "ExtendedOptions: To support FEATURE_ORDERED_DISPLAY, " diff --git a/src/com/android/bitmap/view/BitmapDrawableImageView.java b/src/com/android/bitmap/view/BitmapDrawableImageView.java index b776c34..67ef3eb 100644 --- a/src/com/android/bitmap/view/BitmapDrawableImageView.java +++ b/src/com/android/bitmap/view/BitmapDrawableImageView.java @@ -47,18 +47,23 @@ public class BitmapDrawableImageView extends ImageView { } /** - * Get the source BasicBitmapDrawable for this BitmapDrawableImageView. - * @return The source drawable. + * Get the source drawable for this BitmapDrawableImageView. + * @return The source drawable casted to the given type, or null if the type does not match. */ - public BasicBitmapDrawable getBasicBitmapDrawable() { - return mDrawable; + @SuppressWarnings("unchecked") // Cast to type parameter. + public <E extends BasicBitmapDrawable> E getTypedDrawable() { + try { + return (E) mDrawable; + } catch (Exception ignored) { + return null; + } } /** - * Set the given BasicBitmapDrawable as the source for this BitmapDrawableImageView. + * Set the given drawable as the source for this BitmapDrawableImageView. * @param drawable The source drawable. */ - public void setBasicBitmapDrawable(BasicBitmapDrawable drawable) { + public <E extends BasicBitmapDrawable> void setTypedDrawable(E drawable) { super.setImageDrawable(drawable); unbindDrawable(); mDrawable = drawable; |