Skip to content

Instantly share code, notes, and snippets.

@kingargyle
Created August 21, 2014 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingargyle/f4c53572496f5df9076b to your computer and use it in GitHub Desktop.
Save kingargyle/f4c53572496f5df9076b to your computer and use it in GitHub Desktop.
A runable that handles only transitioning and crossfading when the bitmaps are different.
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.preference.PreferenceManager;
import android.view.View;
/**
* A runnable that transitions between a drawable and a bitmap.
*
* It supports not doing transitions when the bitmaps are the same.
*
* It currently depends on setting transitions at all if there is a
* fade in transition preference enabled in the app. Remove that
* section of code if you don't need this support.
*
* If transition support isn't enabled then it doesn't do transitions
* and just sets the bitmap as the background to the view passed in
* the constructor.
*
* Code licensed under an MIT license so do what you want just give
* attribution where it was obtained.
*
*
* @author davidcarver
*
*/
public class BackgroundBitmapDisplayer implements Runnable {
protected Bitmap bitmap;
protected int defaultImageId;
protected View backgroundView;
public BackgroundBitmapDisplayer(Bitmap bitmap, int defaultImage, View view) {
this.bitmap = bitmap;
this.defaultImageId = defaultImage;
this.backgroundView = view;
}
@Override
public void run() {
if (bitmap == null) {
backgroundView.setBackgroundResource(defaultImageId);
return;
}
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(backgroundView.getContext());
boolean shouldFadeIn = preferences.getBoolean(
"animation_background_fadein", false);
BitmapDrawable bitmapdrawable = new BitmapDrawable(backgroundView
.getContext().getResources(), bitmap);
if (!shouldFadeIn) {
backgroundView.setBackgroundDrawable(bitmapdrawable);
return;
}
crossfadeImages(bitmapdrawable);
}
private void crossfadeImages(BitmapDrawable newBitmapDrawable) {
Drawable currentDrawable = backgroundView.getBackground();
if (currentDrawable instanceof TransitionDrawable) {
currentDrawable = ((TransitionDrawable) currentDrawable)
.getDrawable(1);
}
if (currentDrawable == null) {
currentDrawable = backgroundView.getContext().getResources()
.getDrawable(defaultImageId);
}
Drawable[] drawables = { currentDrawable, newBitmapDrawable };
TransitionDrawable transitionDrawable = new TransitionDrawable(
drawables);
backgroundView.setBackgroundDrawable(transitionDrawable);
transitionDrawable.setCrossFadeEnabled(true);
int crossfadeduration = 200;
if (currentDrawable instanceof BitmapDrawable) {
BitmapDrawable currentBitmapDrawable = (BitmapDrawable) currentDrawable;
if (currentBitmapDrawable.getBitmap().sameAs(
newBitmapDrawable.getBitmap())) {
transitionDrawable.setCrossFadeEnabled(false);
crossfadeduration = 0;
}
}
transitionDrawable.startTransition(crossfadeduration);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment