我想检查 Imageview 是否有特定的 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.在设置drawable之前,在 ImageView 中将drawable id设置为标签,并比较该标签以确定是否是同一图像。
image.setTag(R.drawable.apicture);
检查
if ((int) image.getTag() == R.drawable.apicture) {
// do your stuff
}
2.如果drawable相同则需要匹配
Drawable drawable=image.getDrawable();
Drawable drawable1=context.getDrawable(R.drawable.apicture);
if(drawable.equals(drawable1)){
image.setImageResource(R.drawable.anotherpicture);
}
关于java - 比较 Imageview 和 R.drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41741371/