Skip to content

Instantly share code, notes, and snippets.

@TWiStErRob
Last active October 24, 2016 15:15
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 TWiStErRob/0c33ece48e890407ce47 to your computer and use it in GitHub Desktop.
Save TWiStErRob/0c33ece48e890407ce47 to your computer and use it in GitHub Desktop.
Glide 3.6.0 proof of concept for loading a Drawable as Bitmap through Glide
class DrawableBitmapResourceDecoder implements ResourceDecoder<Drawable, Bitmap> {
private final BitmapPool pool;
public DrawableBitmapResourceDecoder(Context context) {
this(Glide.get(context).getBitmapPool());
}
public DrawableBitmapResourceDecoder(BitmapPool pool) {
this.pool = pool;
}
@Override public Resource<Bitmap> decode(Drawable drawable, int width, int height) throws IOException {
Config config = PixelFormat.formatHasAlpha(drawable.getOpacity())? Config.ARGB_8888 : Config.RGB_565;
Bitmap bitmap = pool.get(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), config);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return new BitmapResource(bitmap, pool);
}
@Override public String getId() { return "DrawableBitmapResourceDecoder"; }
}
GenericRequestBuilder<Drawable, Drawable, Bitmap, ?> glide = Glide
.with(context)
.using(new PassthroughModelLoader<Drawable, Drawable>(), Drawable.class)
.from(Drawable.class)
.as(Bitmap.class)
// transcode helps if you want a drawable in the end, but also want to apply BitmapTransformations
// optional .transcode(new BitmapToGlideDrawableTranscoder(context), GlideDrawable.class)
.decoder(new DrawableBitmapResourceDecoder(context))
.cacheDecoder(new FileToStreamDecoder<Bitmap>(new StreamBitmapDecoder(context)))
.encoder(new BitmapEncoder())
// or .diskCacheStrategy(DiskCacheStrategy.NONE) instead of last 2
;
// simulate a drawable input coming from somewhere
Drawable drawable = getResources().getDrawable(android.R.drawable.sym_def_app_icon);
glide
.clone() // if you use the same glide multiple times (e.g. in Adapter)
.load(drawable)
.signature(new StringSignature("android.R.drawable.sym_def_app_icon")) // required for cache to be correct
// ... add whatever you want here (transformation, animation ...)
.into(imageView)
;
class PassthroughModelLoader<Result, Source extends Result> implements ModelLoader<Source, Result> {
@Override public DataFetcher<Result> getResourceFetcher(final Source model, int width, int height) {
return new CastingDataFetcher<Source, Result>(model); // upcasting is ok
}
/** Extremely unsafe, use with care. */
private static class CastingDataFetcher<Source, Result> implements DataFetcher<Result> {
private final Source model;
public CastingDataFetcher(Source model) {
this.model = model;
}
@SuppressWarnings("unchecked")
@Override public Result loadData(Priority priority) throws Exception {
return (Result) model;
}
@Override public void cleanup() { }
@Override public String getId() { return ""; }
@Override public void cancel() { }
}
}
@TWiStErRob
Copy link
Author

@rameshvoltella
Copy link

hi it a nice solution first thank you for the solution

am using this snippet to load the app icons of apk (which not installed) which is in sd card. i have able to load the drawable of app icon in recyclerview but when scroll wrong image is assigned to the corresponding data also same image is listed multiple time. when scroll it change image changes.
how to fix this issue i used exactly the same code from your gist

@rameshvoltella
Copy link

hi i fixed the issue by using BitmapBitmapResourceDecoder.java
Thank you for BitmapBitmapResourceDecoder
👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment