【问题标题】:Palette using with Glide sometimes fail to load dark vibrant color使用 Glide 的调色板有时无法加载深色鲜艳的颜色
【发布时间】:2016-03-14 20:29:09
【问题描述】:

我正在使用 palette-v7:23.2.1glide:3.7.0,如下所述,但有时是 dark 鲜艳的颜色没有成功提取,我得到了默认颜色。

在我清除滑翔的缓存并再次尝试使用相同的图像后,我得到了正确的颜色。奇怪的是,总是能提取出明亮的鲜艳颜色,但不会提取深色。

可能是什么问题以及如何解决?


在 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 --
    }
};

【问题讨论】:

  • github.com/bumptech/glide/wiki/…这个链接给出了这个问题的原因和解决方案
  • 不推荐我使用的第一个解决方案,因为第二个异步调用可能会更新错误的 ViewHolder,但这不是我的情况。我也试过第二个例子,但有时它不会提取第二种颜色,或者在相同的场景和相同的图像中提取第一种颜色。

标签: android palette android-glide


【解决方案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
        }
    });;

【讨论】:

  • link的解决方案基本相同。我也试过了。我知道,提取可能会因某些图片而失败,但有时会因相同的图片而失败。这就是问题所在。但感谢您提供完整的解决方案。
猜你喜欢
  • 2017-04-26
  • 2014-04-28
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
相关资源
最近更新 更多