【问题标题】:How to Compare Two ImageViews?如何比较两个 ImageView?
【发布时间】:2017-10-14 21:46:21
【问题描述】:

我想通过从 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 版本吗?

【问题讨论】:

    标签: android background drawable


    【解决方案1】:

    尝试比较它们的 BitmapDrawable :

      Bitmap bitmap = ((BitmapDrawable)imgView1.getDrawable()).getBitmap();
        Bitmap bitmap2 = ((BitmapDrawable)imgView2.getDrawable()).getBitmap();
    
      if(bitmap == bitmap2)
         {
    //Code blcok
           }
    

    【讨论】:

    • 你的示例 idk 有问题,为什么,当我用我的 imageview 变量名替换 imgView1 时它不起作用,我应该像这样使用它:imgView1.getBackground() 吗?
    • 对不起,你应该使用:Bitmap bitmap = ((BitmapDrawable)imgView1.getDrawable()).getBitmap();
    • 效果很好!但仍然需要知道他们都使用的是什么图片?是苹果吗?还是橙色?所以当我尝试获取他们当前的背景时,它不适用于 API 26 或 API 21
    • 我知道了,我只需要获取保存在我的 Drawable 中的我的图像的位图。并将其与我的 imageview 的当前图像进行比较。
    • 很好,你能分享一下解决方案的代码吗:这会很有帮助
    猜你喜欢
    • 2011-09-16
    • 2016-02-02
    • 2021-09-21
    • 2012-01-25
    • 2020-03-19
    • 2014-10-29
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多