如何在ImageView中获取当前drawable并将其转换为位图? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我有一个动态的,可点击的ImageView。基本上,一旦用户单击ImageView,他/她就可以添加要在视图内显示的图像。我想找到一种方法来获取ImageView中的当前图像并将其转换为位图。

到目前为止,这是我的尝试:

Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), (BitmapDrawable)imageView.getDrawable());

但它给了我一个Wrong 2nd argument type错误,因为它期待一个int。

为了清楚起见,我现在保持简短的职位。我可以为每个请求发布更多代码。

android android-bitmap
1个回答
1
投票

使用此方法获取Bitmap:

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

decodeResource为您提供资源的位图,例如,如果您想从drawable文件夹中读取图像,则需要decodeResource,但在这种情况下,您可以直接从imageview获取位图

© www.soinside.com 2019 - 2024. All rights reserved.