diff options
author | Mark Wei <markwei@google.com> | 2013-10-30 14:23:50 -0700 |
---|---|---|
committer | Mark Wei <markwei@google.com> | 2013-10-31 17:35:05 -0700 |
commit | 2e4d0863dba53435372ec96538f2ef3e1c3675bf (patch) | |
tree | 6d7bbae6bcb78563036ffe5f685ef1a334bd512c /sample/src | |
parent | 22955165fab693684cc3614c84ee81883ae933c8 (diff) | |
download | bitmap-2e4d0863dba53435372ec96538f2ef3e1c3675bf.tar.gz |
Make ExtendedBitmapDrawable extend BasicBitmapDrawable.
Modify sample app to use ExtendedBitmapDrawable with the following features:
Parallax,
Placholder and progress,
Decode aggregator.
Change-Id: Ia9e0fe6e6fdab018077a465ddf2a40109efbddf7
Diffstat (limited to 'sample/src')
3 files changed, 67 insertions, 6 deletions
diff --git a/sample/src/com/example/bitmapsample/BitmapRequestKeyImpl.java b/sample/src/com/example/bitmapsample/BitmapRequestKeyImpl.java index 6c375f1..eeec78f 100644 --- a/sample/src/com/example/bitmapsample/BitmapRequestKeyImpl.java +++ b/sample/src/com/example/bitmapsample/BitmapRequestKeyImpl.java @@ -27,6 +27,8 @@ public class BitmapRequestKeyImpl implements RequestKey { public final String mUriString; public final URL mUrl; + private boolean mSlept; + public BitmapRequestKeyImpl(String uriString) { mUriString = uriString; URL url = null; @@ -36,6 +38,7 @@ public class BitmapRequestKeyImpl implements RequestKey { e.printStackTrace(); } mUrl = url; + mSlept = false; } @Override @@ -70,6 +73,20 @@ public class BitmapRequestKeyImpl implements RequestKey { @Override public InputStream createInputStream() throws IOException { + // Artificially sleep for (deterministically) random amount of time. + if (!mSlept) { + // Character difference between shortest and longest uri. + final long spread = 26; + // Maximum amount of time to sleep. + final long max = 2; + final long duration = (long) ((float) (mUriString.length() % spread) / spread * max + * 1000); + try { + Thread.sleep(duration); + } catch (InterruptedException ignored) { + } + mSlept = true; + } return mUrl.openStream(); } diff --git a/sample/src/com/example/bitmapsample/BitmapView.java b/sample/src/com/example/bitmapsample/BitmapView.java index f192963..0613168 100644 --- a/sample/src/com/example/bitmapsample/BitmapView.java +++ b/sample/src/com/example/bitmapsample/BitmapView.java @@ -17,13 +17,19 @@ package com.example.bitmapsample; import android.content.Context; +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; public class BitmapView extends BitmapDrawableImageView { - private float mDensity; + private final float mDensity; + + private ListView mListView; + private float mParallaxSpeedMultiplier; public BitmapView(Context c) { this(c, null); @@ -41,6 +47,24 @@ public class BitmapView extends BitmapDrawableImageView { @Override protected void onSizeChanged(final int w, final int h, int oldw, int oldh) { - getBasicBitmapDrawable().setDecodeDimensions(w, h); + ExtendedBitmapDrawable drawable = (ExtendedBitmapDrawable) getBasicBitmapDrawable(); + drawable.setDecodeDimensions(w, (int) (h * mParallaxSpeedMultiplier)); + } + + public void setListView(final ListView listView) { + mListView = listView; + } + + @Override + protected void onDraw(final Canvas canvas) { + ExtendedBitmapDrawable drawable = (ExtendedBitmapDrawable) getBasicBitmapDrawable(); + 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 4ad18e5..96e99ec 100644 --- a/sample/src/com/example/bitmapsample/MainActivity.java +++ b/sample/src/com/example/bitmapsample/MainActivity.java @@ -17,6 +17,8 @@ package com.example.bitmapsample; import android.app.Activity; +import android.content.res.Resources; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; @@ -24,19 +26,33 @@ import android.widget.BaseAdapter; import android.widget.ListView; import com.android.bitmap.BitmapCache; +import com.android.bitmap.DecodeAggregator; import com.android.bitmap.UnrefedBitmapCache; -import com.android.bitmap.drawable.BasicBitmapDrawable; +import com.android.bitmap.drawable.ExtendedBitmapDrawable; public class MainActivity extends Activity { + private ListView mListView; + private final BitmapCache mCache = new UnrefedBitmapCache(TARGET_CACHE_SIZE_BYTES, 0.1f, 0); + private final DecodeAggregator mDecodeAggregator = new DecodeAggregator(); + private static Drawable PLACEHOLDER; + private static Drawable PROGRESS; + + private static final float NORMAL_PARALLAX_MULTIPLIER = 1.5f; private static final int TARGET_CACHE_SIZE_BYTES = 5 * 1024 * 1024; - private final BitmapCache mCache = new UnrefedBitmapCache(TARGET_CACHE_SIZE_BYTES, 0.1f, 0); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + if (PLACEHOLDER == null) { + Resources res = getResources(); + PLACEHOLDER = res.getDrawable(R.drawable.ic_placeholder); + PROGRESS = res.getDrawable(R.drawable.progress); + } + mListView = (ListView) findViewById(R.id.list); mListView.setAdapter(new MyAdapter()); } @@ -91,9 +107,13 @@ public class MainActivity extends Activity { v = (BitmapView) convertView; } else { v = new BitmapView(MainActivity.this); - final BasicBitmapDrawable drawable = new BasicBitmapDrawable(getResources(), mCache, - true /* limit density */); + final ExtendedBitmapDrawable drawable = new ExtendedBitmapDrawable(getResources(), + mCache, true /* limit density */, mDecodeAggregator, PLACEHOLDER, PROGRESS); + drawable.setParallaxSpeedMultiplier(NORMAL_PARALLAX_MULTIPLIER); + v.setBasicBitmapDrawable(drawable); + v.setListView(mListView); + v.setParallaxSpeedMultiplier(NORMAL_PARALLAX_MULTIPLIER); } v.getBasicBitmapDrawable().bind(new BitmapRequestKeyImpl(mItems[position])); return v; |