Android比较imageView和image

Mic*_*ael 3 android drawable imageview

我想将当前 imageView 与 R.drawable 中的图像进行比较。我想我已经尝试了一切,但我无法解决这个问题。我尝试了堆栈溢出的所有方法。

XML:

<ImageView
        android:layout_width="match_parent"
        android:src="@drawable/red"
        android:id="@+id/imageview1"
        android:clickable="true"
        android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

安卓:

final ImageView test = (ImageView) findViewById(R.id.imageview1);

test.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          //if(test.getResources().equals(R.drawable.red))
          //if(test.getDrawable().equals(R.drawable.red))
          if(test.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.red).getConstantState()))
          {
               Toast.makeText(getApplicationContext(), "work", Toast.LENGTH_SHORT).show();
          }
          else
          {
               Toast.makeText(getApplicationContext(), "not work", Toast.LENGTH_SHORT).show();
          }
     }
});
Run Code Online (Sandbox Code Playgroud)

Viv*_*art 6

如果你想保存一些信息以便查看,可以使用标签。

    <ImageView
    android:layout_width="match_parent"
    android:id="@+id/imageview1"
    android:src="@drawable/red"
    android:tag="work"
    android:clickable="true"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

现在你可以比较

        ImageView image = (ImageView) findViewById(R.id.imageview1);
    if ("work".equals(image.getTag())){
        Toast.makeText(getApplicationContext(), "work", Toast.LENGTH_SHORT).show();
    }
Run Code Online (Sandbox Code Playgroud)

您可以从代码中设置此标签

image.setTag("not work");
Run Code Online (Sandbox Code Playgroud)


Mic*_*ael 5

谢谢莫里森,就是这样。

第一的

final ImageView test = (ImageView) findViewById(R.id.imageview1);
final Bitmap bmap = ((BitmapDrawable)test.getDrawable()).getBitmap();
Drawable myDrawable = getResources().getDrawable(R.drawable.red);
final Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
Run Code Online (Sandbox Code Playgroud)

下一个

if(bmap.sameAs(myLogo))
{
do sthng
}
Run Code Online (Sandbox Code Playgroud)