You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I'm using Glide to load a set of pictures from the Android Mediastore in a RecyclerView, along with colors that are extracted from each picture with the Android Palette API.
For that purpose, I defined the following classes :
PaletteBitmap to hold data
PaletteBitmapResource the equivalent Glide Resource,
PaletteBitmapTranscoder, that takes the loaded ResourceBitmap and generate its corresponding color palette.
Here is the code :
// PaletteBitmap.javapublicclassPaletteBitmap {
publicfinalBitmapbitmap;
publicfinalPalettepalette;
// Constructor omitted for brevity
}
// PaletteBitmapResource.javapublicclassPaletteBitmapResourceimplementsResource<PaletteBitmap> {
privatefinalPaletteBitmappaletteBitmap;
privatefinalBitmapPoolbitmapPool;
// This class is the Glide v4 equivalent of the one presented on the Wiki
}
// PaletteBitmapTranscoder.javapublicclassPaletteBitmaptranscoderimplementsTranscoder<Bitmap, PaletteBitmap> {
privatefinalBitmapPoolbitmapPool;
@OverridepublicResource<PaletteBitmap> transcode(Resource<Bitmap> toTranscode) {
Bitmapbitmap = toTranscode.get();
Palettepalette = Palette.from(bitmap).generate();
PaletteBitmapresult = newPaletteBitmap(bitmap, palette);
returnnewPaletteBitmapResource(result, bitmapPool);
}
}
// MyGlideModule.java@GlideModulepublicclassMyAppGlideModuleextendsGlideAppModule {
@OverridepublicvoidregisterComponents(@NullableContextcontext, @NullableGlideglide, @NullableRegistryregistry) {
if (glide != null && registry != null) {
registry.register(Bitmap.class, PaletteBitmap.class, newPaletteBitmapTranscoder(glide.getBitmapPool());
}
}
}
// Then in my AdapterGlideApp.with(fragment).as(PaletteBitmap.class).load(mediastoreUri).into(newImageViewTarget<PaletteBitmap>(holder.imageView) {
@OverrideprotectedvoidsetResource(PaletteBitmapresource) {
// Set the bitmap and use the palette
}
});
This works fine.
What I'd like to achieve is to specify custom options for the Palette.Builder defined in PaletteBitmapTranscoderon a request basis. I mean, something like that :
In Glide v3, I created a new PaletteBitmapTranscoder with my custom options and passed it to the transcode method of RequestBuilder. But in Glide v4, transcoders are registered one for all and shared for all requests.
My question is : Is it possible to do this with the Generated API ?
If yes, can you give me some hints on how to do it ?
If not, is there any other way to handle Palette with Glide that allow this behaviour ?
Thanks in advance, Glide is a great library.
The text was updated successfully, but these errors were encountered:
Not currently, I need to change the API of ResourceTranscoder to pass in Options like the other classes. I think that's straight forward, so I'll take a look.
You can pass in a palette config as an option. For an example, you can take a look at the options in Downsampler. Basically Options is a map of Option keys to Object values. In your case you'll want a memory option since the results of ResourceTranscoders aren't written to disk.
Check out GlideExtension as well. It would allow you to inline your palette options into Glide's generated API, so you'd probably even be able to avoid the apply call.
Glide Version: 4.1.1
Hello, I'm using Glide to load a set of pictures from the Android Mediastore in a RecyclerView, along with colors that are extracted from each picture with the Android Palette API.
For that purpose, I defined the following classes :
Here is the code :
This works fine.
What I'd like to achieve is to specify custom options for the
Palette.Builder
defined inPaletteBitmapTranscoder
on a request basis. I mean, something like that :In Glide v3, I created a new
PaletteBitmapTranscoder
with my custom options and passed it to thetranscode
method of RequestBuilder. But in Glide v4, transcoders are registered one for all and shared for all requests.My question is : Is it possible to do this with the Generated API ?
If yes, can you give me some hints on how to do it ?
If not, is there any other way to handle Palette with Glide that allow this behaviour ?
Thanks in advance, Glide is a great library.
The text was updated successfully, but these errors were encountered: