2016-06-28 49 views
0

我正在創建一個壁紙應用程序,並且我想將Palette類的getLightMutedColor()應用於每個壁紙下面的文本框。但是它重新啓動應用程序會產生問題。如何將顏色應用於GridView中的文本框?

因此,這是應該的

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

我從來沒有使用過這個,但我認爲它可能是從這'int vbcol = p.getLi ghtMutedColor(defaultColor);」你可以添加一個斷點,並認可什麼是錯的? –

+0

我從來沒有使用過斷點。但我試過了,它在文本框上顯示白色背景。 – r2d2arto

+0

我不是這樣,但你可以在AsyncTask中嘗試這個... –

回答

0

這是異步wayt做到這一點,你可以看看這個: https://developer.android.com/reference/android/support/v7/graphics/Palette.html

試試這個代碼,也許它會工作

Palette.from(bitmap).generate(new PaletteAsyncListener() { 
    public void onGenerated(Palette p) { 
     int defaultColor= 0x000000; 
     int vbcol = p.getLightMutedColor(defaultColor); 
     holder.titleTextView.setBackgroundColor(vbcol); 
    } 
}); 
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() { 

     } 
    }); 
相關問題