Skip to content

Instantly share code, notes, and snippets.

@TWiStErRob
Last active August 29, 2015 14:20
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/a981979bd08f48b6d654 to your computer and use it in GitHub Desktop.
Save TWiStErRob/a981979bd08f48b6d654 to your computer and use it in GitHub Desktop.
Glide 3.6.0 proof of concept for loading a Drawable as Drawable through Glide
class DrawableResoureDecoder implements ResourceDecoder<Drawable, Drawable> {
@Override public Resource<Drawable> decode(Drawable source, int width, int height) throws IOException {
return new DrawableResource<Drawable>(source) {
@Override public int getSize() { return 1; }
@Override public void recycle() { }
};
}
@Override public String getId() { return "DrawableDrawableResourceDecoder"; }
}
GenericRequestBuilder<Drawable, Drawable, Drawable, Drawable> glide = Glide
.with(context)
.using(new PassthroughModelLoader<Drawable, Drawable>(), Drawable.class)
.from(Drawable.class)
.as(Drawable.class)
.decoder(new DrawableResoureDecoder())
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE) // can't cache because there's now way to write any Drawable to a File
;
// 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)
// ... 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

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