获取附加到带背景的 ImageView 的位图

Get Bitmap attached to ImageView with background

我提到了 this SO 问题,该问题建议通过这样做从 ImageView 获取位图:

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

我面临的问题是,当我首先设置 ImageView 的 background,然后设置 src,然后使用建议的解决方案时,我只得到位图作为 src 图片。它不包含 ImageViewbackground 部分。我在这里错过了什么?

尝试使用 DrawingCache 获取位图.. 试试下面的代码:

image.setDrawingCacheEnabled(true);

// this is the important code :)  
// Without it the view will have a dimension of 0,0 and the bitmap will be null          
image.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
image.layout(0, 0, image.getMeasuredWidth(), image.getMeasuredHeight()); 

image.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(image.getDrawingCache());
image.setDrawingCacheEnabled(false); // clear drawing cache

您可以通过

获取图像图层
Bitmap bitmap = ((BitmapDrawable)((LayerDrawable)imageViewPlayPause.getDrawable()).getDrawable(0)).getBitmap(‌​);

通过索引 0 和 1 获取图像,并检查您获取的图像。