如何比较两个 ImageView?

How to Compare Two ImageViews?

我想通过从 Drawable 获取背景来检查两个 imageView 是否匹配。

我确实用过这种方式:

     if (imgView1.getBackground().getConstantState()
    .equals(ContextCompat.getDrawable(getApplicationContext(),R.drawable.myImage)
    .getConstantState())
     &&
    imgView2.getBackground().getConstantState()
.equals(ContextCompat.getDrawable(getApplicationContext(),R.drawable.myImage)
    .getConstantState()))
        {


        // do something 
        }

在 API 23 和 API 24 上效果很好,但不适用于 API 21 和 API 26 ? 有没有其他方法让它适用于所有 android 版本?

尝试比较两者的 BitmapDrawable :

  Bitmap bitmap = ((BitmapDrawable)imgView1.getDrawable()).getBitmap();
    Bitmap bitmap2 = ((BitmapDrawable)imgView2.getDrawable()).getBitmap();

  if(bitmap == bitmap2)
     {
//Code blcok
       }