将位图从灰度恢复到原始

时间:2015-01-04 12:29:37

标签: android

我有一个位图,我使用此代码将其设为灰度:

 grayscale.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                   BitmapDrawable  abmp = (BitmapDrawable)img.getDrawable();
                   bmp = abmp.getBitmap();
                          operation= Bitmap.createBitmap(bmp.getWidth(),
                          bmp.getHeight(),bmp.getConfig());
                          Canvas c = new Canvas(operation);
                          Paint paint = new Paint();
                          ColorMatrix cm = new ColorMatrix();
                          cm.setSaturation(0);
                          ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
                          paint.setColorFilter(f);
                          c.drawBitmap(bmp, 0, 0, paint);
                          img.setImageBitmap(operation);
            }
          });

我想把它恢复到原来的状态..有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

设置灰度测试:

Drawable drawable = imageView.getDrawable();

        if (drawable != null) {
            ColorMatrix grayMatrix = new ColorMatrix();
            grayMatrix.setSaturation(0);
            ColorMatrixColorFilter grayScaleFilter = new ColorMatrixColorFilter(
                    grayMatrix
            );
            drawable.setColorFilter(grayScaleFilter);
        }

而不是在油漆上设置它。然后删除:

Drawable d = img.getDrawable();

if(d != null){
    d.setColorFilter(null);
}