android - 在 Android 中对从网络下载的图像进行着色
问题描述
我必须下载图像,将图像着色,使其变暗,然后将其设置为LinearLayout
. 我尝试了以下代码:
Picasso.get().load(url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
bitmapDrawable.setColorFilter(new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN));
linearLayout.setBackground(bitmapDrawable);
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
问题是看不到图像,背景已经完全变黑。如何显示变暗的图像?
解决方案
Use the PorterDuff.Mode.OVERLAY
:
bitmapDrawable.setColorFilter(new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.OVERLAY));
You can read how it works exactly here. Hope this helps!
推荐阅读
- mips - MIPS:将 int 存储为字符串或字符
- list - OCaml 中的递归:使用 Concat 进行反向
- flutter - Flutter - 翻转动画 - 根据点击的位置将卡片翻转到其右侧或左侧
- python - 刮无硒,蟒蛇
- python - 打开并显示从本地文件夹中选择的图像
- kubernetes - 用 Deployment 替换 ReplicationController 是否安全
- c# - c#中受保护和私有保护有什么区别?
- algorithm - 具有两个独立外循环和一个相关内循环的三重嵌套 For 循环
- node.js - 如何使用 MongoDB/Mongoose 和 node.js 中的另一个字段值更新字段
- javascript - 总是收到“无法读取 null 的属性'样式'”错误,我不明白为什么