android - 如何比较两个 ImageView?

标签 android background drawable

我想通过从 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
       }

关于android - 如何比较两个 ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46749651/

相关文章:

java - 更改自定义 ProgressBar 的大小

android - 是否可以使用可绘制对象制作动画?

android - 在android应用程序中实现登录窗口

android - 单标签多 View

android - 如何拖放多个 Activity ?

java - onPause() NullPointerException - 尝试将数据写入数据库

ios - Cordova 灰色背景色

css - 为什么我的 iframe 会复制源页面的背景颜色?

CSS - 为什么我的背景颜色没有在 IE 中显示?

xamarin.android - Visual Studio 2017 中的 Xamarin.Android 项目中的 Drawable 文件夹替换为 mipmap 文件夹?