drawable == drawable?

前端 未结 5 524
盖世英雄少女心
盖世英雄少女心 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: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.

提交回复
热议问题