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