Last active
August 29, 2015 14:20
-
-
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
This file contains 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
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"; } | |
} |
This file contains 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
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 | |
; |
This file contains 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
// 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) | |
; |
This file contains 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
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
See bumptech/glide#122