如何检查ImageView中是否存在特定图像

时间:2017-05-28 19:35:53

标签: android imageview

我想知道如果特定图像停留在imageview中。

像这样的东西

Hello World!

3 个答案:

答案 0 :(得分:2)

getConstantState() drawable方法返回一个Drawable.ConstantState实例,该实例保存此drawable的共享状态。

if (imageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.image1).getConstantState()) {
      Toast.makeText(_con, "Image is image1", Toast.LENGTH_LONG).show();
} else {
     Toast.makeText(_con, "Image isn't image1", Toast.LENGTH_LONG).show(); \
}

答案 1 :(得分:1)

你可以尝试这样:

ImageView v = (ImageView)findViewbyId(R.id.img);
String backgroundImageName = String.valueOf(v.getTag());    

if( backgroundImageName.equals(ImageName))
{
   //do something
} 
else{
   //do something
}

答案 2 :(得分:0)

这里没有直接的答案,你可以在设置资源时做settag这样的事情,这样你就知道了资源的名称,

类似的东西:

imageView.setTag(R.drawable.drawablename);
imageView.setImageResource(R.drawable.drawablename);
String backgroundImageName = String.valueOf(imageView.getTag());

而不是检查:

if (backgroundImageName.equals("BG"))  // "BG" is the tag that you set previously
    {
      Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show(); 
    } 
  else 
    {
     Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show(); 
    }