【问题标题】:How to use Palette API to generate color dynamically?如何使用 Palette API 动态生成颜色?
【发布时间】:2016-06-01 06:36:11
【问题描述】:

浏览网站上的其他教程发现了一个将 Material Design 应用到 Android 应用程序的不错选择,它根据卡片中查看的图片选择 cardView 操作区域颜色。

this toturial 中,他们将图像从 drawable 导入到 ciew 中,我通过创建图像地址的 arrayList 来从网站检索它,然后使用 picasso 将其输入到 cardView 中,几乎就像与教程类似,不同之处在于我没有使用不同的类来解包信息并将其转换为 ArrayList。

因此,当尝试应用教程中演示的相同方法为 placeNameHolder(cardView 中图片下方的操作区域)选择颜色时,我对如何以与我的资源兼容的方式这样做感到困惑?

如果有任何关于如何实现这一点和理解该方法的说明,我将不胜感激?

教程中的Activity_details

   @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_places, parent, false);
return new ViewHolder(view);
 }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {
final Place place = new PlaceData().placeList().get(position);

holder.placeName.setText(place.name);
Picasso.with(mContext).load(place.getImageResourceId(mContext)).into(holder.placeImage);

Bitmap photo = BitmapFactory.decodeResource(mContext.getResources(), place.getImageResourceId(mContext));

Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
  public void onGenerated(Palette palette) {
    int mutedLight = palette.getMutedColor(mContext.getResources().getColor(android.R.color.black));
    holder.placeNameHolder.setBackgroundColor(mutedLight);
  }
});
 }

 @Override
  public int getItemCount() {
return new PlaceData().placeList().size();
}

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public LinearLayout placeHolder;
public LinearLayout placeNameHolder;
public TextView placeName;
public ImageView placeImage;

public ViewHolder(View itemView) {
  super(itemView);
  placeHolder = (LinearLayout) itemView.findViewById(R.id.mainHolder);
  placeName = (TextView) itemView.findViewById(R.id.placeName);
  placeNameHolder = (LinearLayout) itemView.findViewById(R.id.placeNameHolder);
  placeImage = (ImageView) itemView.findViewById(R.id.placeImage);
  placeHolder.setOnClickListener(this);
} 

在我的应用中;

 public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView name;
    public ImageView picture;
    public LinearLayout placeNameHolder;

    public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.item_card, parent, false));

        picture = (ImageView) itemView.findViewById(R.id.placeImage);
        name = (TextView) itemView.findViewById(R.id.card_title);
        placeNameHolder = (LinearLayout) itemView.findViewById(R.id.placeNameHolder);
    }
}
/**
 * Adapter to display recycler view.
 */
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {

    private Context mContext;

    public ContentAdapter(Context context) {

        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        Picasso.with(mContext).load(mImages[position]).into(holder.picture);

        holder.name.setText(author[position]);

        Bitmap photo = BitmapFactory.decodeResource(mImages[position]);

        Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
            public void onGenerated(Palette palette) {
                int bgColor = palette.getMutedColor(mContext.getResources().getColor(android.R.color.black));
                holder.placeNameHolder.setBackgroundColor(bgColor);
            }
        });
    }

不同的是我的资源是;

          Firebase ref = new Firebase("https://wi4x4.firebaseio.com/data/images");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            if(snapshot !=null) {
                for (DataSnapshot child : snapshot.getChildren()) {
                    Log.i("MyTag", child.getValue().toString());
                    imagesfeedsList.add(child.child("address").getValue(String.class));
                    authorfeedsList.add(child.child("author").getValue(String.class));
                }
                Log.i("MyTag_imagesDirFinal", imagesfeedsList.toString());

                mImages = imagesfeedsList.toArray(new String[imagesfeedsList.size()]);
                author = authorfeedsList.toArray(new String[authorfeedsList.size()]);

【问题讨论】:

    标签: android android-cardview android-palette


    【解决方案1】:

    好吧,稍微深入研究一下 stackoverflow 丰富的内容,发现这个post 与我想要实现的目标相似。以及我能够生成的代码;

         try{
                String url1 = mImages[position];
                URL ulrn = new URL(url1);
                HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
                InputStream is = con.getInputStream();
                Bitmap bmp = BitmapFactory.decodeStream(is);
                if (null != bmp)
    
                    Palette.generateAsync(bmp, new Palette.PaletteAsyncListener() {
                        public void onGenerated(Palette palette) {
                            int bgColor = palette.getVibrantColor(mContext.getResources().getColor(android.R.color.black));
                            holder.placeNameHolder.setBackgroundColor(bgColor);
                        }
                    });
                else
                    Log.e("MyTag_BMP","The Bitmap is NULL");
    
            }catch (Exception e){
    
            }
    

    虽然在我的代码中,.getColor 已被弃用?我不知道为什么和结果,几乎是我所期望的。希望对代码进行任何评论以纠正可能出现的问题?

    【讨论】:

    • 你可以使用getColor(res, theme)ContextCompat.getColor(context, res)
    猜你喜欢
    • 2020-03-29
    • 2016-01-20
    • 2017-08-16
    • 1970-01-01
    • 2017-07-12
    • 2023-03-16
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多