如何将颜色应用于GridView中的文本框?

时间:2016-06-28 08:36:12

标签: android android-studio gridview palette

我正在创建一个壁纸应用程序,我想将Palette类的getLightMutedColor()应用到每个壁纸下面的文本框中。但它在重新启动应用程序时会产生问题。

所以这应该是

So this is how it should be

重新启动应用时,颜色会变为白色

On restarting the app the color changes to white

这是我的适配器类

public class GridViewAdapter extends ArrayAdapter<GridItem> {

private Context mContext;
private int layoutResourceId;
private ArrayList<GridItem> mGridData = new ArrayList<GridItem>();

public GridViewAdapter(Context mContext, int layoutResourceId, ArrayList<GridItem> mGridData) {
    super(mContext, layoutResourceId, mGridData);
    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.mGridData = mGridData;
}


/**
 * Updates grid data and refresh grid items.
 * @param mGridData
 */
public void setGridData(ArrayList<GridItem> mGridData) {
    this.mGridData = mGridData;
    notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final ViewHolder holder;

    if (row == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.titleTextView = (TextView) row.findViewById(R.id.grid_item_title);
        holder.imageView = (ImageView) row.findViewById(R.id.grid_item_image);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    GridItem item = mGridData.get(position);
    holder.titleTextView.setText(Html.fromHtml(item.getTitle()));
    Picasso.with(mContext)
            .load(item.getImage())
            .into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    Palette p = Palette.from(bitmap).generate();
                    int defaultColor= 0x000000;
                    int vbcol = p.getLightMutedColor(defaultColor);
                    holder.titleTextView.setBackgroundColor(vbcol);
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });
    Picasso.with(mContext).load(item.getImage()).into(holder.imageView);
    return row;
}

static class ViewHolder {
    TextView titleTextView;
    ImageView imageView;
}
 }

2 个答案:

答案 0 :(得分:0)

这是执行此操作的异步方法,您可以查看以下内容: https://developer.android.com/reference/android/support/v7/graphics/Palette.html

试试这段代码,也许它会起作用

android:windowSoftInputMode="stateHidden|adjustResize"

答案 1 :(得分:0)

对所有内容都获得了nullpointerexception,因此可以确定图像没有被加载,因此我可以使用此回调

        @Override
        public void onSuccess() {
            BitmapDrawable bd = (BitmapDrawable)holder.imageView.getDrawable();
            Bitmap bm =bd.getBitmap();
            Palette p = Palette.from(bm).generate();
            int defcol=0x000000;
            holder.titleTextView.setBackgroundColor(p.getLightVibrantColor(defcol));
        }

        @Override
        public void onError() {

        }
    });