我想检查图像视图是否具有特定的R.drawable,如果是,请用另一个图像更改它。但它不是这样工作的。
XML代码
<ImageView
android:id="@+id/Picture"
android:src="@drawable/apicture"
</ImageView>
Java代码
Image = (ImageView) findViewById(R.id.Picture);
public void coolMethod()
{
if(Image.equals(R.drawable.apicture){
Image.setImageResource(R.drawable.anotherpicture);
}
}
可以有多种方法来实现这一点
1.在设置可绘制对象之前,在图像视图中将可绘制对象ID设置为标签,并比较此标签以确定它是否是同一图像。
image.setTag(R.drawable.apicture);
要检查
if ((int) image.getTag() == R.drawable.apicture) {
// do your stuff
}
2.如果绘制对象相同,则需要匹配它们
Drawable drawable=image.getDrawable();
Drawable drawable1=context.getDrawable(R.drawable.apicture);
if(drawable.equals(drawable1)){
image.setImageResource(R.drawable.anotherpicture);
}