获取ImageView中drawable的ID
我有一个 ImageView 并在其上设置了一个可绘制对象。现在我需要动态获取 ImageView
的点击事件上的可绘制对象的 ID。我怎样才能得到它?
imgtopcolor = (ImageView) findViewById(R.id.topcolor);
imgtopcolor.setImageResource(R.drawable.dr); // How do I get this back?
现在,在 imgtopcolor
的触摸事件中,我想要需要可绘制 id,因为我每次都设置不同的可绘制,并希望将可绘制与其他可绘制进行比较
I have one ImageView
and set a drawable on it. Now I need to get the ID of the drawable on click event of ImageView
dynamically. How can I get it?
imgtopcolor = (ImageView) findViewById(R.id.topcolor);
imgtopcolor.setImageResource(R.drawable.dr); // How do I get this back?
Now on touch event of imgtopcolor
i want to need drawable id because I am setting different drawable each time and want to compare the drawable with other
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想如果我理解正确的话这就是你正在做的事情。
正确的?因此该函数
getDrawableId()
不存在。您无法获取实例化可绘制对象的 id,因为该 id 只是设备上有关如何构造可绘制对象的数据位置的引用。一旦构建了可绘制对象,它就无法取回用于创建它的resourceId。但是你可以使用标签让它像这样工作I think if I understand correctly this is what you are doing.
Right? So that function
getDrawableId()
doesn't exist. You can't get a the id that a drawable was instantiated from because the id is just a reference to the location of data on the device on how to construct a drawable. Once the drawable is constructed it doesn't have a way to get back the resourceId that was used to create it. But you could make it work something like this using tags我已经在另一个问题中回答了类似的问题,但对于这个问题会稍微改变一下。
不幸的是,没有
getImageResource()
或getDrawableId()
。但是,我使用 ImageView 标签创建了一个简单的解决方法。在 onCreate() 中:
然后,如果您愿意,您可以创建一个简单的函数来获取可绘制的 id:
太简单了。
I answered something like this in another question already, but will change it just a little for this one.
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:
Too easy.
截至目前,尚不支持此功能。然而,我发现了这个问题的一个小技巧。
所以如果你想获取视图的ID,只需获取它的标签即可。
As of today, there is no support on this function. However, I found a little hack on this one.
So if you want to get the ID of the view, just get it's tag.
在 StackOverflow 中挖掘类似问题的答案时,我发现人们通常建议两种方法:
那。
就我个人而言,出于性能原因,我喜欢第二种方法,但使用适当的标签标记一堆视图既痛苦又耗时。在一个大项目中这可能会非常令人沮丧。就我而言,我需要编写大量 Espresso 测试,需要比较
TextView
可绘制对象、ImageView
资源、View
背景和前景。很多工作。所以我最终想出了一个解决方案,将“脏”工作委托给定制充气机。在每个膨胀视图中,我都会搜索特定属性,并为视图设置一个带有资源 ID 的标签(如果找到)。这种方法与 Calligraphy 的人使用的方法几乎相同。我为此编写了一个简单的库: TagView
如果您使用它,您可以检索任何预定义的标签,包含在 xml 布局文件中设置的可绘制资源 ID:
实际上,该库支持任何属性。您可以手动添加它们,只需查看 Github 上的自定义属性部分即可。
如果您在运行时设置可绘制对象,则可以使用方便的库方法:
在这种情况下,标记是在内部为您完成的。如果您使用 Kotlin,您可以编写一个方便的扩展来调用视图本身。像这样的事情:
您可以在运行时标记中找到更多信息
Digging StackOverflow for answers on the similar issue I found people usually suggesting 2 approaches:
that.
Personally, I like the second approach for performance reason but tagging bunch of views with appropriate tags is painful and time consuming. This could be very frustrating in a big project. In my case I need to write a lot of Espresso tests which require comparing
TextView
drawables,ImageView
resources,View
background and foreground. A lot of work.So I eventually came up with a solution to delegate a 'dirty' work to the custom inflater. In every inflated view I search for a specific attributes and and set a tag to the view with a resource id if any is found. This approach is pretty much the same guys from Calligraphy used. I wrote a simple library for that: TagView
If you use it, you can retrieve any of predefined tags, containing drawable resource id that was set in xml layout file:
The library supports any attribute, actually. You can add them manually, just look into the Custom attributes section on Github.
If you set a drawable in runtime you can use convenient library methods:
In this case tagging is done for you internally. If you use Kotlin you can write a handy extensions to call view itself. Something like this:
You can find additional info in Tagging in runtime
我最近遇到了同样的问题。我通过实现我自己的 ImageView 类解决了这个问题。
这是我的 Kotlin 实现:
I recently run into the same problem. I solved it by implementing my own ImageView class.
Here is my Kotlin implementation:
一个简单的解决方案可能是将可绘制 id 存储在临时变量中。我不确定这对于您的情况有多实用,但这绝对是一个快速解决方案。
A simple solution might be to just store the drawable id in a temporary variable. I'm not sure how practical this would be for your situation but it's definitely a quick fix.
更简单:只需将
R.drawable
id 存储在视图的 id 中:使用v.setId()
。然后使用v.getId()
取回它。Even easier: just store the
R.drawable
id in the view's id: usev.setId()
. Then get it back withv.getId()
.