ImageView tint 属性及PorterDuff.Mode

本文详细介绍了如何在Android中使用setColorFilter和PorterDuff.Mode来给图片添加蒙层,实现各种视觉效果。通过不同的Mode选项,如ADD、DARKEN、MULTIPLY等,可以实现添加、清除、加深、源像素替换等各种图像合成效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概念

偶然机会要给一个图片添加一个蒙层,半透明-淡淡的雾状效果。于是用到setColorFilter,这个颜色过滤器使用到了android.graphics.PorterDuff.Mode ,于是去官网看下具体使用。很多文章把tint解释为着色。

  • Tint: 着色,就是当前设置的颜色和原目标关联,形成最终的UI效果。
  • Mode:原数据和目标数据它们之间处于什么样的关系。
  String tint = ”#44ffffff“;
  img.setColorFilter(Color.parseColor(tint) );

图解

蓝色是原图片,红色目标图片
sourceImg destinationImg

1. add

Adds the source pixels to the destination pixels and saturates the result.
添加原数据到目标数据,且重叠部分饱和颜色、
add

2. clear

Destination pixels covered by the source are cleared to 0.
清除所有
在这里插入图片描述

2. DARKEN

Retains the smallest component of the source and destination pixels.
保留两者最小的颜色分量
在这里插入图片描述

2. DST

The source pixels are discarded, leaving the destination intact.
保留目标像素
在这里插入图片描述

2. DST_ATOP

Discards the destination pixels that are not covered by source pixels.
丢失没有被原数据覆盖的部分像素
在这里插入图片描述

2.DST_IN

Keeps the destination pixels that cover source pixels, discards the remaining source and destination pixels.
保留覆盖原数据的目标像素,丢弃其他。
在这里插入图片描述

2. DST_OUT

Keeps the destination pixels that are not covered by source pixels.
保留没有被原数据覆盖的像素

在这里插入图片描述

2. DST_OVER

The source pixels are drawn behind the destination pixels.
原数据在目标数据后面
在这里插入图片描述

2. LIGHTEN

Retains the largest component of the source and destination pixel.
保留两者最大的颜色分量
在这里插入图片描述

2. MULTIPLY

Multiplies the source and destination pixels.
混合重叠像素
在这里插入图片描述

2. OVERLAY

Multiplies or screens the source and destination depending on the destination color.
混合两种
在这里插入图片描述

2. SCREEN

Adds the source and destination pixels, then subtracts the source pixels multiplied by the destination.
在这里插入图片描述

2. SRC

The source pixels replace the destination pixels.

在这里插入图片描述

2. SRC_ATOP

Discards the source pixels that do not cover destination pixels.
在这里插入图片描述

2. SRC_IN

Keeps the source pixels that cover the destination pixels, discards the remaining source and destination pixels.
在这里插入图片描述

2. SRC_OUT

Keeps the source pixels that do not cover destination pixels.
在这里插入图片描述

2. SRC_OVER

The source pixels are drawn over the destination pixels.
在这里插入图片描述

2. XOR

Discards the source and destination pixels where source pixels cover destination pixels.
在这里插入图片描述

### Android 中 PorterDuff.Mode 的用法 `PorterDuff.Mode` 是 Android 提供的一个类,用于定义图像混合模式。它主要用于 `Canvas` 绘图操作以及视图背景颜色或图片的叠加效果处理。通过设置不同的 `PorterDuff.Mode` 值,可以实现多种视觉效果。 #### 主要用途 1. **Drawable 颜色过滤**: 使用 `setColorFilter()` 方法为 Drawable 设置颜色滤镜。 2. **Bitmap 合成**: 在 Canvas 上绘制多个 Bitmap 时应用合成模式。 3. **View 背景着色**: 修改 View 的背景颜色并应用特定的效果。 以下是常见的几种 `PorterDuff.Mode` 及其作用: | Mode | 描述 | |-----------------|----------------------------------------------------------------------| | CLEAR | 输出透明像素,忽略源和目标的颜色[^2] | | SRC | 完全显示源图像,忽略目标图像[^2] | | DST | 显示目标图像,忽略源图像[^2] | | SRC_OVER | 默认行为,源图像覆盖在目标图像之上[^2] | | DST_OVER | 目标图像覆盖在源图像之上[^2] | | SRC_IN | 源图像仅保留与目标图像重叠的部分[^2] | | DST_IN | 目标图像仅保留与源图像重叠的部分[^2] | #### 示例代码:Drawable 颜色过滤 下面是一个简单的例子,展示如何使用 `PorterDuff.Mode.SRC_ATOP` 来改变 ImageView 图标的颜色。 ```java ImageView imageView = findViewById(R.id.image_view); Drawable drawable = ContextCompat.getDrawable(this, R.drawable.icon); if (drawable != null) { drawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); // 将图标变为红色 imageView.setImageDrawable(drawable); } ``` #### 示例代码:Button 背景颜色修改 如果希望动态更改 Button 的背景颜色,并应用一种特殊的混合模式,可以这样做: ```java Button button = findViewById(R.id.button); Drawable background = button.getBackground(); if (background != null) { background.setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY); // 应用 Multiply 效果 button.setBackground(background); } ``` 以上代码会将按钮的原始背景颜色与蓝色相乘,从而创建一个新的渐变效果。 --- ### 注意事项 - 不同设备可能对某些复杂的 `PorterDuff.Mode` 支持程度不同,在实际开发中需注意兼容性测试。 - 如果需要更高级的功能(如自定义 Shader),建议结合 OpenGL 或其他图形库来完成复杂渲染任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值