比较Imageview和R.drawable

3
我想检查一个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个回答

3

有多种方法可以实现这一目标

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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接