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 }
兩種效果如下:
原圖:
黑白圖: