Skip to content

Instantly share code, notes, and snippets.

@keyboardr
Created May 7, 2014 10:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyboardr/a16a3ae6364a48bff207 to your computer and use it in GitHub Desktop.
Save keyboardr/a16a3ae6364a48bff207 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri;
import android.util.Log;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Picasso.LoadedFrom;
import com.squareup.picasso.Target;
public class UriDrawable extends LayerDrawable {
private Resources mResources;
public UriDrawable(Context context, Uri uri, Drawable placeholder) {
this(context, uri, placeholder, null);
}
public UriDrawable(Context context, Uri uri, int placeholderResId) {
this(context, uri, context.getResources().getDrawable(placeholderResId));
}
public UriDrawable(Context context, Uri uri, int placeholderResId,
int errorResId) {
this(context, uri,
context.getResources().getDrawable(placeholderResId), context
.getResources().getDrawable(errorResId));
}
public UriDrawable(Context context, Uri uri, Drawable placeholder,
Drawable error) {
super(new Drawable[] { placeholder });
Picasso picasso = Picasso.with(context);
picasso.setDebugging(BuildConfig.DEBUG);
mResources = context.getResources();
picasso.load(uri).placeholder(placeholder).error(error).into(mTarget);
}
private Target mTarget = new Target() {
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
setDrawableByLayerId(getId(0), placeHolderDrawable);
invalidateSelf();
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
setDrawableByLayerId(getId(0), new BitmapDrawable(mResources,
bitmap));
invalidateSelf();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
setDrawableByLayerId(getId(0), errorDrawable);
invalidateSelf();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment