2015-02-24 29 views
3

2014年10月,杰克沃顿写了Coercing Picasso to Play With Palette。在这里面,有2种方法被思考:毕加索加载后,如何为我的位图获取调色板?

  1. 使用变换生成该调色板。这样做的好处是在Picasso的工作线程上生成了调色板,并且在加载图像时准备好。然而,缺点似乎是我得到的调色板与图片不符。目前,我一次只运行其中一种转换。

  2. 使用CallbackonSuccess得到来自ImageView位图并生成调色板。我忍不住专注于杰克在这一行末尾的// Ew!,我完全同意这一点。

上面提到的帖子是在2014年10月写的。现在我们已经过了几个月的讨论,我想知道什么是推荐的方法? 还有关于将meta data与位图关联的讨论。这是否实施?我将如何使用它?

Transformation代码(不是最终的,我刚开始看这个):

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

回答

9

据杰克沃顿有尚未最后我什么没有什么好办法

做的事情是我用了Transformation方法,将难看的代码封装在阁楼里。

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

在我的活动,我做的东西沿着这些路线:

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

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

-1
Target target = new Target() { 
     @Override 
     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
      viewHolder.mItemImage.setImageBitmap(bitmap); 
      Drawable image = viewHolder.mItemImage.getDrawable(); 
     } 

     @Override 
     public void onPrepareLoad(Drawable placeHolderDrawable) { 

     } 

     @Override 
     public void onBitmapFailed(Drawable errorDrawable) { 

     } 
    }; 
    Picasso.with(viewHolder.mItemImage.getContext()).load("url").into(viewHolder.mItemImage); 
+0

这里没有提到调色板或调色板的生成。我不认为你读过这个问题。 – copolii 2015-02-24 21:22:14

+0

这种方法最好在加载位图后启动调色板生成**。我将不得不将其放回到后台线程,并将生成的Palette(如果有)发布到UI线程以将Palette应用于该活动。 – copolii 2015-02-24 21:33:10

+0

我不明白,为什么downvote,你得到的位图,然后你可以调用调色板代从那里...... – ddog 2015-02-25 08:06:34