绘制==绘制? [英] drawable == drawable?

查看:204
本文介绍了绘制==绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题...

在我的活动我有一个ImageView的和一个按钮。我希望按钮只有当ImageView的显示一定的绘制执行操作。是的,这意味着ImageView的是各种可绘制之间为codeD,使得它不与我想要完成中断动画。

In my activity I have an ImageView and a Button. I want the Button to perform an action ONLY when the ImageView displays a certain 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.

这是我辉煌的解决方案:

This was my brilliant solution:

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
    }

奇怪的是,它没有工作。而且我已经把范围缩小到行:如果(variabledrawable == acertaindrawable)的故障。虽然Eclipse不公然报告错误,Android无法识别如果两个可绘制是一样的,我测试过的code中的其他领域都给人以做工精细。

Surprisingly, it didn't work. And I've narrowed it down to the fault of the line "if (variabledrawable == acertaindrawable)". While Eclipse does not blatantly report errors that Android cannot recognise if two drawables are the same, I've tested the other areas of the code and all seem to work fine.

样的灵魂在那里,请你帮帮我!如果没有一个解决方案,我很想知道一个解决办法,以及!在此先感谢!

Kind souls out there, please do help me! If there isn't a solution I'd love to know of a workaround as well! Thanks in advance!

推荐答案

作为解释的Itsik,即使这两个变量包含对对象'看'一样,他们是2个不同的对象实例。

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

的==操作符比较引用。只有当两个变量指向同一个对象实例即返回TRUE。相同的内存空间。

The == operator compares references. It returns true only if both variables refer to the same object instance ie. the same memory space.

无论是绘制对象,也不BitmapDrawable实现特定的.equals(),可能已经适应检查2实例包含相同的数据的方法,让马蒂亚斯林暗示尝试.equals()将无法正常工作。

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.

你可以做,而不必以延长可绘制以下Itsik的建议,是使用View.setTag()和View.getTag()方法。这些方法允许你选择的任何对象附加到一个视图,并在以后检索。通过将一个简单的标识符(可能是一个技术性整数标识符或URL定义位图的来源),每次你改变它的内容到您的ImageView,你就可以很容易地识别它。

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.

这篇关于绘制==绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆