Created
May 7, 2014 10:15
-
-
Save keyboardr/a16a3ae6364a48bff207 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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