将Imageview与R.drawable进行比较

时间:2017-01-19 12:04:28

标签: java android

我想检查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 个答案:

答案 0 :(得分:0)

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

1.在设置drawable之前,将drawable id设置为图像视图中的标记,并比较此标记以确定它是否是同一图像。

image.setTag(R.drawable.apicture);

检查

if ((int) image.getTag() == R.drawable.apicture) {
      // do your stuff
 }

2.如果相同,则需要匹配drawables

Drawable drawable=image.getDrawable();
Drawable drawable1=context.getDrawable(R.drawable.apicture);
if(drawable.equals(drawable1)){
image.setImageResource(R.drawable.anotherpicture);
}