Skip to content

Instantly share code, notes, and snippets.

@jgilfelt
Created January 30, 2013 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgilfelt/4673472 to your computer and use it in GitHub Desktop.
Save jgilfelt/4673472 to your computer and use it in GitHub Desktop.
Android-BitmapCache (https://github.com/chrisbanes/Android-BitmapCache) CacheableImageView with a fade-in transition display option.
/*******************************************************************************
* Copyright 2011, 2012 Chris Banes. 2013 readyState Software Ltd.
*
* 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.
*******************************************************************************/
package com.your.package.name;
import uk.co.senab.bitmapcache.CacheableBitmapDrawable;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CacheableImageView extends ImageView {
private static final int FADE_DURATION = 200;
private static BitmapDrawable mEmptyDrawable;
private static void onDrawableSet(Drawable drawable) {
if (drawable instanceof TransitionDrawable) {
drawable = ((TransitionDrawable) drawable).getDrawable(1);
}
if (drawable instanceof CacheableBitmapDrawable) {
((CacheableBitmapDrawable) drawable).setBeingUsed(true);
}
}
private static void onDrawableUnset(Drawable drawable) {
if (drawable instanceof TransitionDrawable) {
drawable = ((TransitionDrawable) drawable).getDrawable(1);
}
if (drawable instanceof CacheableBitmapDrawable) {
((CacheableBitmapDrawable) drawable).setBeingUsed(false);
}
}
public CacheableImageView(Context context) {
super(context);
}
public CacheableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setImageDrawable(Drawable drawable) {
final Drawable previousDrawable = getDrawable();
// Set new Drawable
super.setImageDrawable(drawable);
if (drawable != previousDrawable) {
onDrawableSet(drawable);
onDrawableUnset(previousDrawable);
}
}
public void setImageDrawable(Drawable drawable, boolean withFade) {
if (withFade) {
if (mEmptyDrawable == null) mEmptyDrawable = new BitmapDrawable(getResources());
TransitionDrawable fadeInDrawable = new TransitionDrawable(new Drawable[] { mEmptyDrawable, drawable });
setImageDrawable(fadeInDrawable);
fadeInDrawable.startTransition(FADE_DURATION);
} else {
setImageDrawable(drawable);
}
}
@Override
public void setImageResource(int resId) {
final Drawable previousDrawable = getDrawable();
super.setImageResource(resId);
onDrawableUnset(previousDrawable);
}
@Override
public void setImageURI(Uri uri) {
final Drawable previousDrawable = getDrawable();
super.setImageURI(uri);
onDrawableUnset(previousDrawable);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
// Will cause displayed bitmap wrapper to be 'free-able'
setImageDrawable(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment