我如何获得一个调色板为我的位图,一旦毕加索加载它? [英] How do I get a Palette for my bitmap once Picasso Loads it?

查看:405
本文介绍了我如何获得一个调色板为我的位图,一旦毕加索加载它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在2014年10月,杰克沃顿写的强制转换毕加索去玩调色板。在这里面,有被沉吟2种方法:


  1. 使用转换产生的调色板。这样做的优点是调色板上毕加索的工作线程产生的,是的就绪的当图像被加载。它的缺点,但是,似乎是说我越来越不图片相匹配的调色板。目前,我只能运行这些变化在同一时间之一。


  2. 使用回调 的onSuccess 从获取位图的的ImageView 并生成调色板。我不禁专注于 //的E w!的杰克把此行​​的末尾,我完全同意。


上述职位写于2014年10现在,我们过去几个月的讨论,我想知道什么是推荐的方法?
还有一个关于元数据位图关联的讨论。是实施?我将如何使用它?

我的转化 code(不是终点,我刚开始看这个):

 公共final类PaletteGenerator
  实现转型{    私人最终诠释numColors;
    @Getter私人调色板调色板;    @覆盖公共位图转换(最终位图源){
        调色板= NULL;
        最终调色板调色板= numColors> 0
                                ? Palette.generate(源,numColors)
                                :Palette.generate(源);
        返回源;
    }    @覆盖公共字符串键(){
        返回的String.format(Locale.ENGLISH,%S:%D的getClass().getCanonicalName(),numColors);
    }    公共PaletteGenerator(){
        这(0);
    }    公共PaletteGenerator(最终诠释C){
        numColors = C;
    }
}


解决方案

据沃顿商学院的杰克的有没有什么好办法没有

最后我做什么是我去转化法,用封装及放丑code;藏在阁楼上。

 公共final类PaletteGeneratorTransformation
  实现转型{    私有静态最终地图<位图,调色板> CACHE =新的WeakHashMap<> ();
    私人最终诠释numColors;    @覆盖公共位图转换(最终位图源){
        如果(!CACHE.containsKey(源)){
            最终调色板调色板= numColors> 0
                      ? Palette.generate(源,numColors)
                      :Palette.generate(源);
            CACHE.put(来源调色板);
        }        返回源;
    }    @覆盖公共字符串键(){
        返回的getClass().getCanonicalName()+:+ numColors;
    }    公共PaletteGeneratorTransformation(){
        这(0);
    }    公共PaletteGeneratorTransformation(最终诠释C){
        numColors = C;
    }    公共静态抽象类回调
      实现com.squareup.picasso.Callback {
        私人最终目标ImageView的;        公共回调(最终的ImageView T){
            目标= T;
        }        @覆盖公共无效的onSuccess(){
            onPalette(CACHE.get(((BitmapDrawable)target.getDrawable())getBitmap())。);
        }        @覆盖公共无效onerror的(){
            onPalette(NULL);
        }        公共抽象无效onPalette(最终调色板调色板);
    }
}

在我的活动,我做的这些方针的东西:

  Picasso.with(上下文)
    .load(getPhotoUri())
    .transform(新PaletteGeneratorTransformation(DEFAULT_NUM_COLORS))
    .into(照片,新PaletteGeneratorTransformation.Callback(照片){
                        @覆盖公共无效onPalette(最终调色板调色板){
                            themeWithPalette(调色板);
                        }
                    });

剩下的唯一问题是,我知道有丑隐藏在阁楼,现在有保持我的肮脏的小秘密。

In October 2014, Jake Wharton wrote Coercing Picasso to Play With Palette. In it, there are 2 methods being pondered:

  1. Using a Transformation that generates the palette. The advantage of this is that the palette is generated on Picasso's worker thread and is ready when the image is loaded. The disadvantage, however, seems to be that I'm getting palettes that don't match the picture. At the moment I only run one of these transformations at a time.

  2. Using a Callback that onSuccess gets the bitmap from the ImageView and generates the Palette. I can't help but focus on the // Ew! that Jake puts at the end of this line and I quite agree with it.

The post mentioned above was written in October 2014. Now that we're months past the discussion, I'm wondering what is the recommended method? There was also a discussion about associating meta data with bitmaps. Is that implemented? How would I use it?

My Transformation code (not final, I've just started looking into this):

public final class PaletteGenerator
  implements Transformation {

    private final int numColors;
    @Getter private Palette palette;

    @Override public Bitmap transform (final Bitmap source) {
        palette = null;
        final Palette palette = numColors > 0
                                ? Palette.generate (source, numColors)
                                : Palette.generate (source);
        return source;
    }

    @Override public String key () {
        return String.format (Locale.ENGLISH, "%s:%d", getClass ().getCanonicalName (), numColors);
    }

    public PaletteGenerator () {
        this (0);
    }

    public PaletteGenerator (final int c) {
        numColors = c;
    }
}

解决方案

According to Jake Wharton there is no good way yet

What I ended up doing was that I went with the Transformation method, with the ugly code encapsulated & hidden in the attic.

public final class PaletteGeneratorTransformation
  implements Transformation {

    private static final Map<Bitmap, Palette> CACHE = new WeakHashMap<> ();
    private final int    numColors;

    @Override public Bitmap transform (final Bitmap source) {
        if (!CACHE.containsKey (source)) {
            final Palette palette = numColors > 0
                      ? Palette.generate (source, numColors)
                      : Palette.generate (source);
            CACHE.put (source, palette);
        }

        return source;
    }

    @Override public String key () {
        return getClass ().getCanonicalName () + ":" + numColors;
    }

    public PaletteGeneratorTransformation () {
        this (0);
    }

    public PaletteGeneratorTransformation (final int c) {
        numColors = c;
    }

    public static abstract class Callback
      implements com.squareup.picasso.Callback {
        private final ImageView target;

        public Callback (final ImageView t) {
            target = t;
        }

        @Override public void onSuccess () {
            onPalette (CACHE.get (((BitmapDrawable) target.getDrawable ()).getBitmap ()));
        }

        @Override public void onError () {
            onPalette (null);
        }

        public abstract void onPalette (final Palette palette);
    }
}

In my Activity, I do something along these lines:

Picasso.with(context)
    .load (getPhotoUri())
    .transform (new PaletteGeneratorTransformation (DEFAULT_NUM_COLORS))
    .into (photo, new PaletteGeneratorTransformation.Callback (photo) {
                        @Override public void onPalette (final Palette palette) {
                            themeWithPalette (palette);
                        }
                    });

The only problem remaining is that I know there's ugliness hidden in the attic and for now that has to remain my dirty little secret.

这篇关于我如何获得一个调色板为我的位图,一旦毕加索加载它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆