Android设置ImageView图片灰度

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

//灰度/黑白
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);
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/11/android-set-imageview-image-grayscale/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android设置ImageView图片灰度
采用颜色矩阵ColorMatrix和色彩矩阵颜色过滤器ColorMatrixColorFilter实现 //灰度/黑白 public static void grayImage(ImageView imageView) { ColorMatr……
<<上一篇
下一篇>>
文章目录
关闭
目 录