我正在使用 palette-v7:23.2.1 与滑翔:3.7.0 ,如下所述,但有时深色鲜艳的颜色未成功提取,我得到默认颜色。
在我清除滑动缓存并再次尝试使用相同的图像后,我得到了正确的颜色。奇怪的是,总是提取明亮的鲜艳色彩,而不是暗色。
可能是什么问题以及如何解决?
在onCreateView()中:
Glide.with(mContext)
.load(artworkUrl)
.asBitmap()
.into(new BitmapImageViewTarget(mArtworkInToolbar) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
new Palette.Builder(bitmap).generate(paletteAsyncListener);
super.onResourceReady(bitmap, anim);
}
});
监听器:
public final Palette.PaletteAsyncListener paletteAsyncListener =
new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
if (palette == null) return;
int default = ContextCompat.getColor(mContext, R.color.colorPrimary);
int color = palette.getVibrantColor(default); //always ok
int colorDark = palette.getDarkVibrantColor(default); //not always
// --- Setting the color --
}
};
答案 0 :(得分:1)
***Here this is worked for me***
make a class name PaletteBitmap.java
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;
}
Bitmap getBitmap()
{
return bitmap;
}
}
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();
}
}
}
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();
}
}
**Now in Your onCreateView() do like this**
Glide.with(this).fromUri().asBitmap()
.transcode(new PaletteBitmapTranscoder(this),PaletteBitmap.class)
.fitCenter()load(uri).into(new ImageViewTarget<PaletteBitmap>(imageView){
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void setResource(PaletteBitmap resource) {
imageView.setImageBitmap(resource.getBitmap());
int colorP = resource.palette.getMutedColor(ContextCompat.getColor(context, R.color.colorPrimary));
int colorD=resource.palette.getDarkMutedColor(ContextCompat.getColor(context,R.color .colorPrimaryDark));
// dowhatever you want
}
});;