How to Compare Two ImageViews?

99封情书 提交于 2019-12-23 03:03:20

问题


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

i did use this way :

     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 
        }

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?


回答1:


try comparing BitmapDrawable of both of them :

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

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


来源:https://stackoverflow.com/questions/46749651/how-to-compare-two-imageviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!