Android 检查ImageView中使用的背景
我需要能够说出在我的ImageView中使用的背景:Android 检查ImageView中使用的背景,android,android-layout,android-xml,Android,Android Layout,Android Xml,我需要能够说出在我的ImageView中使用的背景: <ImageView android:id="@+id/icon" android:layout_width="24dp" android:layout_height="24dp" android:background="@drawable/ic_globe" /> 如何以编程方式检查上述ImageView的背景是否为ic_globe Bitmap b1 = ((BitmapDrawa
<ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/ic_globe"
/>
如何以编程方式检查上述ImageView的背景是否为ic_globe
Bitmap b1 = ((BitmapDrawable) ((ImageView)findViewById(R.id.icon)).getBackground()).getBitmap();
Bitmap b2 = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_globe)).getBitmap();
if (b1 == b2){
Toast.makeText(MainActivity.this, "Matched", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "NOT Matched", Toast.LENGTH_LONG).show();
}
您需要处理b1和b2的空值,就好像图像无法解码一样,然后您将b1或b2设为空值,您可以实现自己。是否尝试为imageview标记?即setTag(R.drawable.ic_globle)和后者获取标记并比较值,即view.getTag()==R.drawable.ic_globe