AndroidStudio获取ImageView中当前显示的图片与获取setColorFilter(ColorMatrixFilter(colorMatrix))滤镜处理之后的图片

AndroidStudio获取ImageView中当前的图片:

BitmapDrawable mBitmapDrawable = imageView.getDrawable();
Bitmap bitmap = mBitmapDrawable.getBitmap();

或者直接

Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

参考来源:https://blog.csdn.net/wuqingsen1/article/details/84957650

 

但是!!!

你看到的样子不一定就是它当前的位图(亲自踩坑

比如使用setColorFilter()

imgViewB.setImageBitmap(mBitmap);
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.set(mColorMatrixList.get(pos));
imgViewB.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

这样imageViewB中的图片看起来是改变了,但通过上面的方法获取到的还是mBitmap

那么怎么获得这个图片呢?

可以通过创建新的bitmap空白对象,通过paint保存滤镜矩阵,再利用Canvas将图片mBitmap和滤镜重新画上去得到

public Bitmap getChangeBitmap( Bitmap srcBitmap) {

        //创建一个大小相同的空白Bitmap
        Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        //获取颜色矩阵
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(mColorMatrixList.get(finalPos));
        //载入Canvas,Paint
        Canvas canvas = new Canvas(dstBitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        //绘图
        canvas.drawBitmap(srcBitmap, 0, 0, paint);
        return dstBitmap;
    }

颜色矩阵参考简书:https://www.jianshu.com/p/a98beee731bf

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值