浏览网站其他教程找到了一个很好的选择,将材料设计应用到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()]);
答案 0 :(得分:0)
好吧,有点潜入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已被弃用?我不确定为什么和结果,几乎是我所期待的。会不会对代码的任何评论表示感谢,以纠正可能出现的问题?