diff options
-rw-r--r-- | sample/res/layout/activity_main.xml | 4 | ||||
-rw-r--r-- | sample/res/values/dimens.xml | 22 | ||||
-rw-r--r-- | sample/src/com/example/bitmapsample/BitmapView.java | 26 | ||||
-rw-r--r-- | sample/src/com/example/bitmapsample/MainActivity.java | 8 | ||||
-rw-r--r-- | src/com/android/bitmap/drawable/BasicBitmapDrawable.java | 12 | ||||
-rw-r--r-- | src/com/android/bitmap/view/BasicImageView.java | 78 |
6 files changed, 97 insertions, 53 deletions
diff --git a/sample/res/layout/activity_main.xml b/sample/res/layout/activity_main.xml index f182664..1b29402 100644 --- a/sample/res/layout/activity_main.xml +++ b/sample/res/layout/activity_main.xml @@ -18,10 +18,6 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:paddingBottom="@dimen/activity_vertical_margin" - android:paddingLeft="@dimen/activity_horizontal_margin" - android:paddingRight="@dimen/activity_horizontal_margin" - android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView diff --git a/sample/res/values/dimens.xml b/sample/res/values/dimens.xml deleted file mode 100644 index f009504..0000000 --- a/sample/res/values/dimens.xml +++ /dev/null @@ -1,22 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<!-- Copyright (C) 2013 The Android Open Source Project - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. ---> - -<resources> - - <!-- Default screen margins, per the Android Design guidelines. --> - <dimen name="activity_horizontal_margin">16dp</dimen> - <dimen name="activity_vertical_margin">16dp</dimen> -</resources> diff --git a/sample/src/com/example/bitmapsample/BitmapView.java b/sample/src/com/example/bitmapsample/BitmapView.java index 37904f4..d8de0c9 100644 --- a/sample/src/com/example/bitmapsample/BitmapView.java +++ b/sample/src/com/example/bitmapsample/BitmapView.java @@ -17,15 +17,11 @@ package com.example.bitmapsample; import android.content.Context; -import android.os.Build; import android.util.AttributeSet; -import android.view.View; -import com.android.bitmap.BitmapCache; -import com.android.bitmap.drawable.BasicBitmapDrawable; +import com.android.bitmap.view.BasicImageView; -public class BitmapView extends View { - private BasicBitmapDrawable mBitmapDrawable; +public class BitmapView extends BasicImageView { private float mDensity; public BitmapView(Context c) { @@ -44,22 +40,6 @@ public class BitmapView extends View { @Override protected void onSizeChanged(final int w, final int h, int oldw, int oldh) { - mBitmapDrawable.setDecodeDimensions(w, h); + getDrawable().setDecodeDimensions(w, h); } - - public void setImage(String uriString) { - if (mBitmapDrawable != null) { - mBitmapDrawable.bind(new BitmapRequestKeyImpl(uriString)); - } - } - - public void initialize(BitmapCache cache) { - mBitmapDrawable = new BasicBitmapDrawable(getResources(), cache, true /* limitDensity */); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - setBackground(mBitmapDrawable); - } else { - setBackgroundDrawable(mBitmapDrawable); - } - } - }
\ 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 6db2bc5..f580dd8 100644 --- a/sample/src/com/example/bitmapsample/MainActivity.java +++ b/sample/src/com/example/bitmapsample/MainActivity.java @@ -25,7 +25,7 @@ import android.widget.ListView; import com.android.bitmap.BitmapCache; import com.android.bitmap.UnrefedBitmapCache; -import com.android.bitmap.util.Trace; +import com.android.bitmap.drawable.BasicBitmapDrawable; public class MainActivity extends Activity { private ListView mListView; @@ -91,9 +91,11 @@ public class MainActivity extends Activity { v = (BitmapView) convertView; } else { v = new BitmapView(MainActivity.this); - v.initialize(mCache); + final BasicBitmapDrawable drawable = new BasicBitmapDrawable(getResources(), mCache, + true /* limit density */); + v.setDrawable(drawable); } - v.setImage(mItems[position]); + v.getDrawable().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 9cee3c0..b172373 100644 --- a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java +++ b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java @@ -34,6 +34,7 @@ import com.android.bitmap.ReusableBitmap; import com.android.bitmap.util.BitmapUtils; import com.android.bitmap.util.RectUtils; import com.android.bitmap.util.Trace; +import com.android.bitmap.view.BasicImageView; import java.util.concurrent.Executor; import java.util.concurrent.LinkedBlockingQueue; @@ -45,18 +46,23 @@ import java.util.concurrent.TimeUnit; * including request creation/cancelling, and data unbinding and re-binding. * <p> * The actual bitmap decode work is handled by {@link DecodeTask}. + * <p> + * If being used with a long-lived cache (static cache, attached to the Application instead of the + * Activity, etc) then make sure to call {@link BasicBitmapDrawable#unbind()} at the appropriate + * times so the cache has accurate unref counts. The {@link BasicImageView} class has been created + * to do the appropriate unbind operation when the view is detached from the window. */ public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCallback, Drawable.Callback { protected static Rect sRect; protected RequestKey mCurrKey; - protected ReusableBitmap mBitmap; protected final Paint mPaint = new Paint(); private final BitmapCache mCache; private final boolean mLimitDensity; private final float mDensity; + private ReusableBitmap mBitmap; private DecodeTask mTask; private int mDecodeWidth; private int mDecodeHeight; @@ -229,6 +235,10 @@ public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCa @Override public void onDecodeCancel(final RequestKey key) { } + protected ReusableBitmap getBitmap() { + return mBitmap; + } + private void setBitmap(ReusableBitmap bmp) { if (mBitmap != null && mBitmap != bmp) { mBitmap.releaseReference(); diff --git a/src/com/android/bitmap/view/BasicImageView.java b/src/com/android/bitmap/view/BasicImageView.java new file mode 100644 index 0000000..eaaf953 --- /dev/null +++ b/src/com/android/bitmap/view/BasicImageView.java @@ -0,0 +1,78 @@ +package com.android.bitmap.view; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.util.AttributeSet; +import android.widget.ImageView; + +import com.android.bitmap.drawable.BasicBitmapDrawable; + +/** + * A helpful ImageView replacement that can generally be used in lieu of ImageView. + * BasicImageView has logic to unbind its BasicBitmapDrawable when it is detached from the window. + */ +public class BasicImageView extends ImageView { + private BasicBitmapDrawable mDrawable; + + public BasicImageView(final Context context) { + this(context, null); + } + + public BasicImageView(final Context context, final AttributeSet attrs) { + this(context, attrs, 0); + } + + public BasicImageView(final Context context, final AttributeSet attrs, final int defStyle) { + super(context, attrs, defStyle); + } + + /** + * Set the given BasicBitmapDrawable as the source for this BasicImageView. + * @param drawable The source drawable. + */ + public void setDrawable(BasicBitmapDrawable drawable) { + super.setImageDrawable(drawable); + mDrawable = drawable; + } + + public BasicBitmapDrawable getDrawable() { + return mDrawable; + } + + @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + + mDrawable.unbind(); + } + + @Override + public void setImageDrawable(final Drawable drawable) { + throw new UnsupportedOperationException( + "BasicImageView is only compatible with BasicBitmapDrawable. Use setDrawable() " + + "instead."); + } + + @Override + public void setImageResource(final int resId) { + throw new UnsupportedOperationException( + "BasicImageView is only compatible with BasicBitmapDrawable. Use setDrawable() " + + "instead."); + } + + @Override + public void setImageURI(final Uri uri) { + throw new UnsupportedOperationException( + "BasicImageView is only compatible with BasicBitmapDrawable. Use setDrawable() " + + "instead."); + } + + @Override + public void setImageBitmap(final Bitmap bm) { + throw new UnsupportedOperationException( + "BasicImageView is only compatible with BasicBitmapDrawable. Use setDrawable() " + + "instead."); + } +} |