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     }

两种效果如下:

原图:

黑白图:

 

posted on 2018-02-04 14:24  静夜方赤  阅读(1560)  评论(0编辑  收藏  举报

导航