Android设置ImageView图片灰度

采用颜色矩阵ColorMatrix和色彩矩阵颜色过滤器ColorMatrixColorFilter实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//灰度/黑白
public static void grayImage(ImageView imageView) {
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0); //饱和度: 0灰色 100过度彩色 50正常
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageView.setColorFilter(filter);
}

//亮灰度
public static void grayImage(ImageView imageView, int bright) {
ColorMatrix matrix = new ColorMatrix(new float[] {
0.213f, 0.715f, 0.072f, 0, bright,
0.213f, 0.715f, 0.072f, 0, bright,
0.213f, 0.715f, 0.072f, 0, bright,
0, 0, 0, 1f, 0
});
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageView.setColorFilter(filter);
}

//改变亮度
public static void brightImage(ImageView imageView, int bright) {
ColorMatrix matrix = new ColorMatrix(new float[] {
1, 0, 0, 0, bright,
0, 1, 0, 0, bright,
0, 0, 1, 0, bright,
0, 0, 0, 1f, 0
});
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageView.setColorFilter(filter);
}

//恢复
public static void resetImage(ImageView imageView) {
ColorMatrix matrix = new ColorMatrix();
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageView.setColorFilter(filter);
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :