Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检查ImageView中使用的背景_Android_Android Layout_Android Xml - Fatal编程技术网

Android 检查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中使用的背景:

<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