From 2ca4046cd93f8a4385a2d9925fef20a407946543 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Mon, 28 Jul 2014 10:19:18 -0700 Subject: Fix for recycling old transition drawable The old implementation only kept current + 1 old drawable. While this reduced the number of cases where the old drawable was attempted to be used, it did not completely eliminate it. There were still cases where the + 1 old drawable was used and caused a crash. This new implementation just completely clears the old transition drawable from the stack after a certain amount of time. --- src/github/daneren2005/dsub/util/ImageLoader.java | 45 ++++++++++++----------- 1 file changed, 24 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/util/ImageLoader.java b/src/github/daneren2005/dsub/util/ImageLoader.java index dc10ebba..d42fd5b9 100644 --- a/src/github/daneren2005/dsub/util/ImageLoader.java +++ b/src/github/daneren2005/dsub/util/ImageLoader.java @@ -185,13 +185,13 @@ public class ImageLoader { return coverArtId + size; } - private void setImage(View view, Drawable drawable, boolean crossfade) { + private void setImage(View view, final Drawable drawable, boolean crossfade) { if (view instanceof TextView) { // Cross-fading is not implemented for TextView since it's not in use. It would be easy to add it, though. TextView textView = (TextView) view; textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null); } else if (view instanceof ImageView) { - ImageView imageView = (ImageView) view; + final ImageView imageView = (ImageView) view; if (crossfade) { Drawable existingDrawable = imageView.getDrawable(); if (existingDrawable == null) { @@ -202,27 +202,30 @@ public class ImageLoader { emptyImage = Bitmap.createBitmap(imageSizeDefault, imageSizeDefault, Bitmap.Config.ARGB_8888); } existingDrawable = new BitmapDrawable(context.getResources(), emptyImage); - } else { - // Try to get rid of old transitions - try { - TransitionDrawable tmp = (TransitionDrawable) existingDrawable; - int layers = tmp.getNumberOfLayers(); - existingDrawable = tmp.getDrawable(layers - 1); - } catch(Exception e) { - // Do nothing, just means that the drawable is a flat image - } - } - if (!(((BitmapDrawable)existingDrawable).getBitmap().isRecycled())) - { // We will flow through to the non-transition if the old image is recycled... Yay 4.3 - Drawable[] layers = new Drawable[]{existingDrawable, drawable}; - - TransitionDrawable transitionDrawable = new TransitionDrawable(layers); - imageView.setImageDrawable(transitionDrawable); - transitionDrawable.startTransition(250); - return; + } else if(existingDrawable instanceof TransitionDrawable) { + // This should only ever be used if user is skipping through many songs quickly + TransitionDrawable tmp = (TransitionDrawable) existingDrawable; + exisitingDrawable = tmp.getDrawable(tmp.getNumberOfLayers() - 1); } + + Drawable[] layers = new Drawable[] {existingDrawable, drawable}; + final TransitionDrawable transitionDrawable = new TransitionDrawable(layers); + imageView.setImageDrawable(transitionDrawable); + transitionDrawable.startTransition(250); + + // Get rid of transition drawable after transition occurs + imageView.getHandler().postDelayed(new Runnable() { + @Override + public void run() { + // Only execute if still on same transition drawable + if(imageView.getDrawable() == transitionDrawable) { + imageView.setImageDrawable(drawable); + } + } + }, 500L); + } else { + imageView.setImageDrawable(drawable); } - imageView.setImageDrawable(drawable); } } -- cgit v1.2.3