简体   繁体   English

drawable == drawable?

[英]drawable == drawable?

This is my problem...: 这是我的问题......:

In my activity I have an ImageView and a Button . 在我的活动中,我有一个ImageView和一个Button I want the Button to perform an action ONLY when the ImageView displays a certain drawable. 我希望Button仅在ImageView显示某个drawable时执行操作。 And yes, this means that the ImageView is animating between various drawables that is coded such that it does not interrupt with what I want done. 是的,这意味着ImageView在各种drawable之间进行动画处理,这些drawable被编码,使得它不会因我想要的而中断。

ImageView imgview = (ImageView)findViewById(R.id.imgviewsid);
Resources res = getResources();
Drawable acertaindrawable = res.getDrawable(R.drawable.thecertaindrawable);
Drawable variabledrawable = imgview.getDrawable();

    if (variabledrawable == acertaindrawable)
    {
            //does something
    }

It didn't work. 它没用。 And I've narrowed it down to the fault of the line "if (variabledrawable == acertaindrawable)". 我把它缩小到“if(variabledrawable == acertaindrawable)”这一行的错误。 While Eclipse does not blatantly report errors that Android cannot recognize if two drawables are the same, I've tested the other areas of the code and all seem to work fine. 虽然Eclipse没有公然报告Android无法识别的错误,如果两个drawables是相同的,我已经测试了代码的其他区域,所有似乎都工作正常。

I know it's fairly late to post this, but it'll be useful for anyone googling. 我知道发布此内容已经相当晚了,但对任何人用Google进行搜索都会有用。

I used . 我用了 。 getConstantState() to compare my two drawables and it worked like a charm :) getConstantState()比较我的两个drawables,它就像一个魅力:)

As explained by Itsik, even if both variables contain references to objects that 'look' the same, they are 2 different objects instances. 正如Itsik所解释的那样,即使两个变量都包含对“看起来”相同的对象的引用,它们也是两个不同的对象实例。

The == operator compares references. ==运算符比较引用。 It returns true only if both variables refer to the same object instance ie. 仅当两个变量都引用同一个对象实例时才返回true。 the same memory space. 相同的内存空间。

Neither Drawable nor BitmapDrawable implement a specific .equals() method that could have been adapted to check that 2 instances contain the same data, so Mathias Lin hint to try .equals() will not work. Drawable和BitmapDrawable都没有实现一个特定的.equals()方法,该方法可以用于检查2个实例是否包含相同的数据,因此Mathias Lin暗示尝试.equals()将无效。

What you could do, following Itsik's advice without having to extend Drawable, is use the View.setTag() and View.getTag() methods. 按照Itsik的建议而不必扩展Drawable,你可以做的是使用View.setTag()和View.getTag()方法。 These methods allow to attach any Object of your choice to a View and retrieve it later. 这些方法允许将您选择的任何Object附加到View并稍后检索它。 By attaching a simple identifier (be it a technical integer identifier or a url defining the source of the bitmap) to your ImageView each time you change its content, you will be able to recognize it easily. 通过在每次更改其内容时将简单标识符(无论是技术整数标识符还是定义位图源的URL)附加到ImageView,您将能够轻松识别它。

尝试if (acertaindrawable.equals(variabledrawable)) ...

variabledrawable and acertaindrawable aren't the same Object, even though they might display the same drawable. variabledrawable和acertaindrawable不是同一个Object,即使它们可能显示相同的drawable。

If android doesn't give you a built-in way to compare drawables (i guess it depends on the concrete drawable you have), my advice to you is to Extend the Drawable class that you are using and add a private field that holds some kind of ID so that you can compare between drawables. 如果android没有给你一个内置的方法来比较drawables(我猜它取决于你有的具体drawable),我的建议是扩展你正在使用的Drawable类并添加一个包含一些的私有字段一种ID,以便您可以比较drawables。

you can also try: 你也可以尝试:

getId() and setId() :=) getId()和setId():=)

can also be used for comparision if tags are already used for something else :) 如果标签已用于其他东西,也可以用于比较:)

Ps.: but watch out if you're using RelativeLayouts ... :=P Ps。:但请注意你是否使用RelativeLayouts ......:= P.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM