getImageResource()Android.这可能吗? [英] getImageResource() Android. Is this possible?
问题描述
我已经使用setImageResource(R.drawable.icon1)
为ImageView
设置了图像.
I have set an image for an ImageView
using the setImageResource(R.drawable.icon1)
.
现在我的要求是找出为ImageView
设置的图像是什么,并进行一些处理.
Now my requirement is to find out what is the image that is set for an ImageView
and do some processing.
喜欢
if (imageView.getImageResource() == R.drawable.icon1) {
//do some processing
else if (imageView.getImageResource() == R.drawable.icon2) {
//do somethign else
else
//display
所以我想知道是否存在一个方法(AFAIK,它不存在),以及它是否不该如何跟踪为ImageView
设置了哪些资源.
So I would like to know if there exists a method(AFAIK, it doesn't) and if it doesn't how to keep a track of what resources have been set for an ImageView
.
谢谢. 萨那.
推荐答案
您假设,因为您输入了整数,所以可以取回整数,而setImageResource()
则不是这样.请参见 ImageView#setImageResource().这只是为您提供的一种便捷方法:Android在后台进行操作,正在查找Drawable资源(在大多数情况下,它是BitmapDrawable,但可以是任何类型),然后将该资源作为位图对象(即仅图像数据-它不知道其原始资源ID"以前是什么).
You're assuming that because you put an integer in, you are able to get an integer back out, and that's not how setImageResource()
works. See ImageView#setImageResource(). This is just a convenience method for you: what Android is doing behind the scenes, is looking up the Drawable resource (in most cases, it's a BitmapDrawable, but it could be any type), and then applying that resource to the ImageView as a Bitmap object (i.e., image data only -- it does not have any idea what its original "resource id" was previously).
您最好的解决方案是跟踪您使用的最后一个资源ID:
Your best solution is going to be keeping track of the last resource id you used:
imageView.setImageResource(R.drawable.image1);
this.mLastResourceId = R.drawable.image1;
// ...
// Later, when checking the resource:
if (this.mLastResourceId == R.drawable.image1) {
// Do something
}
这篇关于getImageResource()Android.这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!