Android Studio - 如何将 Drawable 中的图像与 R 中的其他 Drawable 进行比较?

Android Studio - How to compare the image within a Drawable to other Drawables in R?

我有一个 children 的应用程序,其中包含大量 ImageButton,每个 ImageButton 显示不同的 randomly-chosen 动物图像。当应用程序说“哪一个是猴子?”时,child 有望推动带有猴子图像的 ImageButton。

然而,为此,我需要能够将 ImageButton 中的图像与 R.drawable 中的目标位图图像进行比较的代码。 Whosebug 上的先前帖子建议解决此问题的方法是在 ImageButtons 中设置标签,但这不是可行的解决方案,原因太长无法解释。

到目前为止,这是我的代码:

private boolean compareImages(ImageButton buttonPushed){
    Drawable drawable = buttonPushed.getDrawable();
    if(drawable.getConstantState().equals(R.drawable.monkey))     // NOT WORKING
        return true;
    return false;
}

在调试器中,我可以看到 equals() 行失败,但不确定原因。我也不确定我是否在比较相同的东西。通过阅读其他 Whosebug 帖子,看起来检查位图的方法是这样的:

    Drawable drawable = buttonPushed.getDrawable();
    drawable.getConstantState()    <--- this points to R.drawable.monkey ???

不确定。有人知道语法秘密吗?

来自上一个非常相似的问题here

试试这个

if(drawable.getConstantState().equals(getResources().getDrawable(R.drawable.monkey).getConstantState())){
       //Do your work here
    }