如何在recyclerview中使用Palette库

时间:2017-09-22 11:40:41

标签: java android android-recyclerview android-cardview

我想使用Palette库从图像中选择一种颜色并在recyclerview中添加到cardview但是我的代码不起作用,我也使用glid来显示来自服务器的图像,如何解决我的问题或使用Palette来在recyclerview中选择一种颜色?谢谢,如果你指导我或给我一些代码

我的代码

public class adapter_zakeran extends
    RecyclerView.Adapter<adapter_zakeran.ItemViewHolder> {

private Context mcontax;
private ArrayList<Item_zakeran> itemlist;
Item_zakeran item;

public adapter_zakeran(Context context, ArrayList<Item_zakeran> itemlist) {
    this.mcontax = context;
    this.itemlist = itemlist;
}

@Override
public adapter_zakeran.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.from(parent.getContext())
            .inflate(R.layout.cardview_list_madahi, parent, false);
    adapter_zakeran.ItemViewHolder itemViewHolder = new adapter_zakeran.ItemViewHolder(view);
    return itemViewHolder;
}

@Override
public void onBindViewHolder(final adapter_zakeran.ItemViewHolder holder, int position) {

    item = itemlist.get(position);

    final int ids = item.getId();

    holder.namemadah.setText(item.getName());

     Glide.with(holder.imagelist.getContext())
            .load(item.getImage())
            .asBitmap()
            .centerCrop()
            .into(new SimpleTarget<Bitmap>(1000,1000) {
                @Override
                public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
                    holder.imagelist.setImageBitmap(bitmap); // Possibly runOnUiThread()
                    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                        @Override
                        public void onGenerated(Palette palette) {
                            Palette.Swatch darkVibrant = palette.getDarkVibrantSwatch();
                            if (darkVibrant != null) {
                                holder.cvItem.setBackgroundColor(darkVibrant.getRgb());
                                holder.namemadah.setBackgroundColor(darkVibrant.getTitleTextColor());
                            } else {
                                //dark vibrant colors don't exist, you can try to find other swatches
                                //or set a default color..
                            }
                        }
                    });
                }
            });


    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(v.getContext(), zakeran_any_madah.class);
            in.putExtra("speaker_id", ids);
            v.getContext().startActivity(in);
        }
    });

}

@Override
public int getItemCount() {
    if (itemlist != null) {
        return itemlist.size();

    }

    return 0;
}


public static class ItemViewHolder extends RecyclerView.ViewHolder {


    public CardView cvItem;
    public TextView namemadah;

    public ImageView imagelist;


    public ItemViewHolder(View itemView) {
        super(itemView);

        cvItem = (CardView) itemView.findViewById(R.id.cardView);
        namemadah = (TextView) itemView.findViewById(R.id.namemadah);


        imagelist = (ImageView) itemView.findViewById(R.id.imagelist);


    }
}

}

错误

FATAL EXCEPTION: main
Process: com.example.kaveh.zakerinekordestan, PID: 28896 java.lang.NullPointerException
at com.example.kaveh.zakerinekordestan.adapter_zakeran$1$1.onGenerated(adapter_zakeran.java:81)
at android.support.v7.graphics.Palette$Builder$1.onPostExecute(Palette.java:873)
at android.support.v7.graphics.Palette$Builder$1.onPostExecute(Palette.java:860)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

要将背景应用于Android中的cardview,请使用card.setCardBackgroundColor(color);

如果您想通过xml使用card_view:cardBackgroundColor="@color/your_color"

更改颜色

因此,对于您的案例使用

holder.cvItem.setCardBackgroundColor(palette.getDarkVibrantSwatch().getRgb());

答案 1 :(得分:0)

根据文档,无法保证可以从图像中提取所有样本。

您的代码应如下所示:

Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
    @Override
    public void onGenerated(Palette palette) {
         Palette.Swatch darkVibrant = palette.getDarkVibrantSwatch();
         if (darkVibrant != null) {
              holder.cvItem.setCardBackgroundColor(darkVibrant.getRgb());
              holder.namemadah.setTextColor(darkVibrant.getTitleTextColor());
         } else {
             //dark vibrant colors don't exist, you can try to find other swatches
             //or set a default color..    
         }
    }
});

Documentation is here

  

PS:你确定要设置你的背景颜色吗?   TextView而不是文字颜色?因为在这种情况下你应该使用   setTextColor()