2015-10-07 54 views
5

我想使用畢加索將Color設置爲佔位符圖像。如何使用顏色作爲佔位符圖像與畢加索?

我嘗試這樣做:

int placeHolderColor2 = Color.rgb(20,20,20); 

    Picasso.with(context) 
     .load(item.getImageUrls().get(0)) 
     .placeholder(placeHolderColor2) 
     .error(R.drawable.card_image) 
     .centerCrop() 
     .fit() 
     .into(viewHolder.imageView); 

但它會導致以下錯誤:

10-07 05:36:42.965 5827-5827/? E/AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0xff141414 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.content.res.Resources.getValue(Resources.java:1266) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.content.res.Resources.getDrawable(Resources.java:785) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.content.res.Resources.getDrawable(Resources.java:752) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at com.squareup.picasso.RequestCreator.getPlaceholderDrawable(RequestCreator.java:676) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at com.squareup.picasso.RequestCreator.into(RequestCreator.java:637) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at com.lorentzos.swipecards.ServiceCardDtoListAdapter.createViewFromResource(ServiceCardDtoListAdapter.java:116) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at com.lorentzos.swipecards.ServiceCardDtoListAdapter.getView(ServiceCardDtoListAdapter.java:66) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at com.lorentzos.flingswipe.SwipeFlingAdapterView.layoutChildren(SwipeFlingAdapterView.java:161) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at com.lorentzos.flingswipe.SwipeFlingAdapterView.refresh(SwipeFlingAdapterView.java:152) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at com.lorentzos.flingswipe.SwipeFlingAdapterView.onLayout(SwipeFlingAdapterView.java:138) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.view.View.layout(View.java:15671) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.view.ViewGroup.layout(ViewGroup.java:5038) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.view.View.layout(View.java:15671) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.view.ViewGroup.layout(ViewGroup.java:5038) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.view.View.layout(View.java:15671) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.view.ViewGroup.layout(ViewGroup.java:5038) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.view.View.layout(View.java:15671) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.view.ViewGroup.layout(ViewGroup.java:5038) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at android.view.View.layout(View.java:15671) 
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:  at 

如何使用一種顏色作爲與畢加索佔位符圖像?

回答

5

您可以從錯誤理解日誌本身android.content.res.Resources$NotFoundException: Resource ID #0xff141414

使用最新版本的Picasso

並嘗試這個

Picasso.with(mContext).load("URL").placeholder(R.color.holder_color).error(R.color.error_color).into(viewHolder.imageView); 
5

在項目中,我用這個解決方案,使顏色佔位符

 gradientDrawable = new GradientDrawable(); 
     gradientDrawable.setShape(GradientDrawable.RECTANGLE); 
     gradientDrawable.setColor(color); 

Picasso.with(context) 
    .load(item.getImageUrls().get(0)) 
    .placeholder(gradientDrawable) 
    .error(R.drawable.card_image) 
    .centerCrop() 
    .fit() 
    .into(viewHolder.imageView); 

這種方法有助於將ImageView設置爲wrap_content,因爲如您所知,顏色不會佔用任何空間,因爲簡單的顏色填充會使圖像不可見,直到圖片加載。