【问题标题】:Compare Imageview with R.drawable [duplicate]比较 Imageview 和 R.drawable [重复]
【发布时间】:2017-07-12 16:56:15
【问题描述】:

我想检查 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);
     }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    可以有多种方法来实现这一点

    1.在设置drawable之前先在image view中设置drawable id为tag,比较这个tag判断是否是同一张图片。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-15
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多