【问题标题】:How to change color of a imageview that I load from Picasso from Green to Red如何将我从毕加索加载的图像视图的颜色从绿色更改为红色
【发布时间】:2018-11-06 01:07:49
【问题描述】:

这是我的代码:

backPic.setScaleType(ImageView.ScaleType.FIT_CENTER);
backPic.setColorFilter(ContextCompat.getColor(context, R.color.red), android.graphics.PorterDuff.Mode.MULTIPLY);
Picasso.with(context).load(icon).into(backPic, new com.squareup.picasso.Callback() {
    @Override
    public void onSuccess() {
    }

    @Override
    public void onError() {
        UserVehicle.setVehicleClassPic(getVclass().getId(), backPic);
    }
});

初始图片是这样的:

这是我现在得到的:

我怎样才能让第二张图片中的自行车是红色的,而不是灰色的绿色?

我发送到图像过滤器的颜色是红色。

【问题讨论】:

  • 您可以使用DrawableCompat.setTint() 更改图像颜色。

标签: android colors imageview picasso tint


【解决方案1】:

使用 André Sousa 的评论,我做了这个代码:

   Picasso.with(context).load(icon).into(backPic, new com.squareup.picasso.Callback() {
                @Override
                public void onSuccess() {
                    DrawableCompat.setTint(backPic.getDrawable(), context.getResources().getColor(R.color.red));
                }

                @Override
                public void onError() {
                    UserVehicle.setVehicleClassPic(getVclass().getId(), backPic);
                }
            });

所以它会在 Picasso 图像加载后设置色调,这很有效

【讨论】:

    【解决方案2】:

    请阅读here:

    问题出在这一行:

      backPic.setColorFilter(ContextCompat.getColor(context, R.color.red), 
      android.graphics.PorterDuff.Mode.MULTIPLY);
    

    具体来说:

      PorterDuff.Mode.MULTIPLY
    

    你想要的:

     PorterDuff.Mode.DST
    

    我相信。但是,如果您阅读我提供的文档,您会看到不同模式的作用。请记住,当您计算颜色时,它们会做一些有趣的事情。将绿色和红色相乘会为您提供时髦的灰色,添加模式本来是我的首选,但这会给您带来时髦的棕色。 DST 用新像素替换旧像素,防止其混合颜色。

    祝你好运!

    【讨论】:

    • DST 返回绿色图像,而不是红色。我现在正在研究所有模式,并会尝试看看哪一种最适合我
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2011-10-28
    • 2017-08-19
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多