Android 图片黑白显示 自定义饱和度

Android 自定义显示黑白色图片

1.先下载下来需要显示的图片(或头像)

我模拟下,将图片放到assert文件夹下,拿到他的InputStream.代码如下:

 1 InputStream in = null;
 2         try {
 3             in = getAssets().open("girl.jpg");
 4         } catch (IOException e) {
 5             if(in != null){
 6                 try {
 7                     in.close();
 8                 } catch (IOException e1) {
 9                     e1.printStackTrace();
10                 }
11             }
12             in = null;
13         }

2.设置到ImageView中去

1 if(in != null){
2         mImageView.setImageBitmap(BitmapFactory.decodeStream(in));
3 }

 

3.写设置饱和度为黑白图的代码

1 public void clickImageBlackWhite(View view) {
2         if(mGrayColorFilter == null){
3             ColorMatrix cm = new ColorMatrix();
4             cm.setSaturation(0f); // 设置饱和度:0为纯黑白,饱和度为0;1为饱和度为100,即原图;
5             mGrayColorFilter = new ColorMatrixColorFilter(cm);
6         }
7         mImageView.setColorFilter(mGrayColorFilter);
8     }

4.写饱和度为原图的代码(这个比较简单,置空或者调整饱和度为100就好)

1 public void clickImageOriginal(View view) {
2         mImageView.setColorFilter(null);
3     }

两种效果如下:

原图:

黑白图:

 

转载于:https://www.cnblogs.com/bokezhilu/p/8413119.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值