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);
}
上一篇 Android TimerTask定时任务卡顿
下一篇 Android组件化开发之ImageLoader封装
目录
文章列表
1 Spring Cloud配置中心和消息总线(配置中心终结版)
Spring Cloud配置中心和消息总线(配置中心终结版)
2
使用 AWS SES 接收电子邮件
使用 AWS SES 接收电子邮件
3
使用第三方工具redis-dump工具对Redis集群所有数据进行导出导入
使用第三方工具redis-dump工具对Redis集群所有数据进行导出导入
4
Redis 安全
Redis 安全
5
PHP 输入流 php://input
PHP 输入流 php://input
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。