From b2ea0d0f28a9c583234fc1d83115b514db7c049f Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sun, 8 Mar 2015 17:28:19 -0700 Subject: Get around the random recycled bitmap errors from calling onDraw after a bitmap had been recycled to make room for a new one. This isn't the ideal fix since it will show a blank image, but it is better than just crashing. --- res/layout-land/download.xml | 2 +- res/layout-large-land/download.xml | 2 +- res/layout-port/download.xml | 2 +- res/layout/abstract_fragment_activity.xml | 2 +- res/layout/album_list_item.xml | 2 +- res/layout/chat_item.xml | 2 +- res/layout/chat_item_reverse.xml | 2 +- res/layout/select_album_header.xml | 2 +- res/layout/user_header.xml | 2 +- res/layout/user_list_item.xml | 2 +- .../daneren2005/dsub/view/RecyclingImageView.java | 57 ++++++++++++++++++++++ .../daneren2005/dsub/view/SquareImageView.java | 2 +- 12 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 src/github/daneren2005/dsub/view/RecyclingImageView.java diff --git a/res/layout-land/download.xml b/res/layout-land/download.xml index e97db866..c0108597 100644 --- a/res/layout-land/download.xml +++ b/res/layout-land/download.xml @@ -21,7 +21,7 @@ android:layout_height="fill_parent" android:layout_weight="1"> - - - - - - - - - - . + Copyright 2015 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.view; + +import android.annotation.TargetApi; +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.util.AttributeSet; +import android.widget.ImageView; + +public class RecyclingImageView extends ImageView { + public RecyclingImageView(Context context) { + super(context); + } + + public RecyclingImageView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public RecyclingImageView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public RecyclingImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + public void onDraw(Canvas canvas) { + Drawable drawable = this.getDrawable(); + if(drawable != null && drawable instanceof BitmapDrawable) { + BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; + if(bitmapDrawable.getBitmap() != null && bitmapDrawable.getBitmap().isRecycled()) { + this.setImageDrawable(null); + } + } + + super.onDraw(canvas); + } +} diff --git a/src/github/daneren2005/dsub/view/SquareImageView.java b/src/github/daneren2005/dsub/view/SquareImageView.java index 77ca50db..44a74e16 100644 --- a/src/github/daneren2005/dsub/view/SquareImageView.java +++ b/src/github/daneren2005/dsub/view/SquareImageView.java @@ -19,7 +19,7 @@ import android.content.Context; import android.util.AttributeSet; import android.widget.ImageView; -public class SquareImageView extends ImageView { +public class SquareImageView extends RecyclingImageView { public SquareImageView(final Context context, final AttributeSet attrs) { super(context, attrs); } -- cgit v1.2.3