我想使用Picasso将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
如何使用Color作为Picasso的占位符图像?
答案 0 :(得分:6)
您可以从错误日志本身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);
答案 1 :(得分: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,因为简单的颜色填充会使图像在加载图片之前不可见,因为正如您所知,颜色不占用任何空间。
答案 2 :(得分:0)
您可以简单地使用类ColorDrawable
。另外,最好在文件colors.xml
中放置颜色:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="your_color">#202020</color>
</resources>
然后您的Java代码将如下所示:
Drawable placeholder = new ColorDrawable(getResources().getColor(R.color.your_color));
Picasso.with(context)
.load(item.getImageUrls().get(0))
.placeholder(placeholder)
.error(R.drawable.card_image)
.centerCrop()
.fit()
.into(viewHolder.imageView);
如果您在2018年使用毕加索,则将with(context
替换为get()
:
Drawable placeholder = new ColorDrawable(getResources().getColor(R.color.your_color));
Picasso.get()
.load(item.getImageUrls().get(0))
.placeholder(placeholder)
.error(R.drawable.card_image)
.centerCrop()
.fit()
.into(viewHolder.imageView);