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;
}