在ImageView android中获取关联图像(Drawable)
我已将图像设置为 android 中的 ImageView
控件:
iv.setImageResource(R.drawable.image1);
我想获取此关联的 Drawable,如下所示:
Drawable myDrawable = iv.getImageResource(); //wrong
I have set an image to an ImageView
control in android:
iv.setImageResource(R.drawable.image1);
I want to get this associated Drawable, looks like:
Drawable myDrawable = iv.getImageResource(); //wrong
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以获得可绘制的资源,例如
您可以将其与可绘制的资源进行比较,例如
You can get the drawable like
You can compare it with a drawable resource like
首先定义Drawable。
Define the Drawable first.
不幸的是,没有
getImageResource()
或getDrawableId()
。但是,我使用 ImageView 标签创建了一个简单的解决方法。在 onCreate():
然后,如果你愿意,你可以创建一个简单的函数来获取可绘制的 id:
我希望这对你有帮助,它确实让我的工作更轻松。
Unfortunately, there is no
getImageResource()
orgetDrawableId()
. But, I created a simple workaround by using the ImageView tags.In onCreate():
Then, if you like, you can create a simple function to get the drawable id:
I hope this helps you, it sure made my work easier.
使用Drawabledrawable=imageview.getDrawable();
use
Drawable drawable=imageview.getDrawable();
您可以比较可绘制资源,例如
从同一资源创建的 BitmapDrawables 将共享存储在其 ConstantState 中的唯一位图
https://developer.android.com/reference/android/graphics/可绘制/可绘制.ConstantState
You can compare drawable resources like
BitmapDrawables created from the same resource will for instance share a unique -bitmap stored in their ConstantState
https://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState
如果您可以使用视图的 id 成员变量,这是一个简单的方法:只需使用 v.setId() 存储 R.drawable id 即可。然后用 v.getId() 取回它。
This is an easy approach if you can use the view's id member variable: just store the R.drawable id using v.setId(). Then get it back with v.getId().
//getConstantState()是关键
//getConstantState() is the key
getResources().getDrawable() 现已弃用,只需使用 getDrawable(R.id.myImage);
getResources().getDrawable() is now deprecated, just use getDrawable(R.id.myImage);