drawable == drawable?

前端 未结 5 434
盖世英雄少女心
盖世英雄少女心 2021-01-03 01:56

This is my problem...:

In my activity I have an ImageView and a Button. I want the Button to perform an action ONLY when the ImageVie

相关标签:
5条回答
  • 2021-01-03 02:27

    I know it's fairly late to post this, but it'll be useful for anyone googling.

    I used .getConstantState() to compare my two drawables and it worked like a charm :)

    0 讨论(0)
  • 2021-01-03 02:39

    As explained by Itsik, even if both variables contain references to objects that 'look' the same, they are 2 different objects instances.

    The == operator compares references. It returns true only if both variables refer to the same object instance ie. 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.

    What you could do, following Itsik's advice without having to extend Drawable, is use the View.setTag() and View.getTag() methods. These methods allow to attach any Object of your choice to a View and retrieve it later. 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.

    0 讨论(0)
  • 2021-01-03 02:50

    Try if (acertaindrawable.equals(variabledrawable)) ...

    0 讨论(0)
  • 2021-01-03 02:50

    you can also try:

    getId() and 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

    0 讨论(0)
  • 2021-01-03 02:51

    variabledrawable and acertaindrawable aren't the same Object, even though they might display the same 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.

    0 讨论(0)
提交回复
热议问题