Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply custom options to transcoder #2417

Closed
thibseisel opened this issue Sep 21, 2017 · 2 comments
Closed

Apply custom options to transcoder #2417

thibseisel opened this issue Sep 21, 2017 · 2 comments
Milestone

Comments

@thibseisel
Copy link

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 :

  • 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.java
public class PaletteBitmap {
    public final Bitmap bitmap;
    public final Palette palette;

    // Constructor omitted for brevity
}

// PaletteBitmapResource.java
public class PaletteBitmapResource implements Resource<PaletteBitmap> {
    private final PaletteBitmap paletteBitmap;
    private final BitmapPool bitmapPool;

   // This class is the Glide v4 equivalent of the one presented on the Wiki
}

// PaletteBitmapTranscoder.java
public class PaletteBitmaptranscoder implements Transcoder<Bitmap, PaletteBitmap> {
    private final BitmapPool bitmapPool;

    @Override public Resource<PaletteBitmap> transcode(Resource<Bitmap> toTranscode) {
        Bitmap bitmap = toTranscode.get();
        Palette palette = Palette.from(bitmap).generate();
        PaletteBitmap result = new PaletteBitmap(bitmap, palette);
        return new PaletteBitmapResource(result, bitmapPool);
    }
}

// MyGlideModule.java
@GlideModule
public class MyAppGlideModule extends GlideAppModule {
    @Override
    public void registerComponents(@Nullable Context context, @Nullable Glide glide, @Nullable Registry registry) {
        if (glide != null && registry != null) {
            registry.register(Bitmap.class, PaletteBitmap.class, new PaletteBitmapTranscoder(glide.getBitmapPool());
        }
    }
}

// Then in my Adapter
GlideApp.with(fragment).as(PaletteBitmap.class).load(mediastoreUri).into(new ImageViewTarget<PaletteBitmap>(holder.imageView) {
    @Override protected void setResource(PaletteBitmap resource) {
        // 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 PaletteBitmapTranscoder on a request basis. I mean, something like that :

GlideApp.with(fragment).as(PaletteBitmap.class).load(mediastoreUri)
        .apply(paletteConfig()
            .maximumColorCount(24)
            .withRegion(...))
        .into(...);

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.

@sjudd
Copy link
Collaborator

sjudd commented Sep 21, 2017

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.

@sjudd sjudd added this to the 4.2 milestone Sep 21, 2017
@sjudd sjudd closed this as completed in 134870e Sep 21, 2017
@sjudd
Copy link
Collaborator

sjudd commented Sep 21, 2017

This will go out in 4.2, by the end of the month.

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.

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

No branches or pull requests

2 participants