Skip to content

Instantly share code, notes, and snippets.

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 rameshvoltella/468ab0c4b77197d145d855882effad8a to your computer and use it in GitHub Desktop.
Save rameshvoltella/468ab0c4b77197d145d855882effad8a 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() { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment