2017-10-14 111 views
0

我想通过从Drawable获取背景来检查两个imageViews是否匹配。如何比较两个ImageViews?

我没有用这种方式:

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

回答

1

尝试比较两者的BitmapDrawable:

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

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

有什么东西不对您的例子IDK的为什么,当我与它不工作我的ImageView变量名称替换imgView1,我应该使用这样的:imgView1.getBackground ()? –

+0

对不起,您应该使用:位图位图=((BitmapDrawable)imgView1.getDrawable())。getBitmap(); – Nawrez

+1

很棒!但仍然需要知道他们都在使用什么图片?它是苹果吗?或橙色?所以当我尝试获取当前背景时,它不适用于API 26或API 21 –