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. --- .../daneren2005/dsub/view/RecyclingImageView.java | 57 ++++++++++++++++++++++ .../daneren2005/dsub/view/SquareImageView.java | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/github/daneren2005/dsub/view/RecyclingImageView.java (limited to 'src/github/daneren2005') diff --git a/src/github/daneren2005/dsub/view/RecyclingImageView.java b/src/github/daneren2005/dsub/view/RecyclingImageView.java new file mode 100644 index 00000000..c4fc7f0a --- /dev/null +++ b/src/github/daneren2005/dsub/view/RecyclingImageView.java @@ -0,0 +1,57 @@ +/* + This file is part of Subsonic. + Subsonic is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + Subsonic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with Subsonic. If not, see . + 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