How can I use a Color as placeholder image with Picasso?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 08:17:39

You can understand from the error log itself android.content.res.Resources$NotFoundException: Resource ID #0xff141414

Use latest version of Picasso

And try this

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

In my project I used this solution to make a color placeholder

        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);

This approach helps when your ImageView is set to wrap_content, as simple color fill will make your image invisible until the picture is loaded, because, as you know, color doesn't occupy any space.

You can simply use class ColorDrawable. Also better to place color in file colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="your_color">#202020</color>
</resources>

And then your java code will look like:

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);

And if you're using Picasso in 2018 then replace with(context with 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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!