Android 图片变暗
时间: 2023-08-05 22:28:34 浏览: 47
在 Android 中,可以通过设置 ImageView 控件的 ColorFilter 来实现图片变暗的效果。
具体实现步骤如下:
1. 在布局文件中添加一个 ImageView 控件,并设置图片资源。
2. 在 Java 代码中获取 ImageView 控件的引用。
3. 创建一个 ColorMatrix 对象,用于控制颜色矩阵。
4. 创建一个 ColorMatrixColorFilter 对象,将 ColorMatrix 对象应用到 ImageView 控件中。
5. 通过 ImageView 的 setColorFilter 方法,将 ColorMatrixColorFilter 对象设置给 ImageView 控件。
以下是代码示例:
```
// 获取 ImageView 控件的引用
ImageView imageView = findViewById(R.id.image_view);
// 创建颜色矩阵和颜色过滤器
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0); // 设置饱和度为0,即变成灰色
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
// 将颜色过滤器设置给 ImageView 控件
imageView.setColorFilter(colorFilter);
```
这段代码会将 ImageView 控件中的图片变成灰色,实现了图片变暗的效果。可以根据自己的需求进行调整。