Created
May 13, 2018 13:44
-
-
Save vsay01/066bebfae7687d503538ac265d9f9e6b to your computer and use it in GitHub Desktop.
Glide Bitmap Palette https://github.com/bumptech/glide/wiki/Custom-targets#palette-example
This file contains hidden or 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
| public class PaletteBitmap { | |
| public final Palette palette; | |
| public final Bitmap bitmap; | |
| public PaletteBitmap(@NonNull Bitmap bitmap, @NonNull Palette palette) { | |
| this.bitmap = bitmap; | |
| this.palette = palette; | |
| } | |
| } |
This file contains hidden or 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
| public class PaletteBitmapResource implements Resource<PaletteBitmap> { | |
| private final PaletteBitmap paletteBitmap; | |
| private final BitmapPool bitmapPool; | |
| public PaletteBitmapResource(@NonNull PaletteBitmap paletteBitmap, @NonNull BitmapPool bitmapPool) { | |
| this.paletteBitmap = paletteBitmap; | |
| this.bitmapPool = bitmapPool; | |
| } | |
| @Override | |
| public PaletteBitmap get() { | |
| return paletteBitmap; | |
| } | |
| @Override | |
| public int getSize() { | |
| return Util.getBitmapByteSize(paletteBitmap.bitmap); | |
| } | |
| @Override | |
| public void recycle() { | |
| if (!bitmapPool.put(paletteBitmap.bitmap)) { | |
| paletteBitmap.bitmap.recycle(); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| public class PaletteBitmapTranscoder implements ResourceTranscoder<Bitmap, PaletteBitmap> { | |
| private final BitmapPool bitmapPool; | |
| public PaletteBitmapTranscoder(@NonNull Context context) { | |
| this.bitmapPool = Glide.get(context).getBitmapPool(); | |
| } | |
| @Override | |
| public Resource<PaletteBitmap> transcode(Resource<Bitmap> toTranscode) { | |
| Bitmap bitmap = toTranscode.get(); | |
| Palette palette = new Palette.Builder(bitmap).generate(); | |
| PaletteBitmap result = new PaletteBitmap(bitmap, palette); | |
| return new PaletteBitmapResource(result, bitmapPool); | |
| } | |
| @Override | |
| public String getId() { | |
| return PaletteBitmapTranscoder.class.getName(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment