如何比较两个ImageViews? [英] How to Compare Two ImageViews?

查看:111
本文介绍了如何比较两个ImageViews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to check if two imageViews match or not , by getting their background from the Drawable .

我确实使用这种方式:

     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版本吗?

it works great on API 23 and API 24 , but not working with API 21 and API 26 ? is there another way to make it work for all android versions?

推荐答案

尝试比较它们两个的BitmapDrawable:

try comparing BitmapDrawable of both of them :

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

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

这篇关于如何比较两个ImageViews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆