如何检查哪些当前图像资源附加到android xml中的ImageView?

时间:2014-04-29 07:01:36

标签: android xml android-imageview

我想检查哪个图像资源附加到xml中的ImageView,我能够检查哪个图像资源附加到图像视图但我的要求是如何检查ImageView我有没有设置成xml的相同资源,基于我需要执行一些actions.Code总是执行else部分。以下是我的代码,

  if (regProfile.getDrawable() == getResources().getDrawable( R.drawable.ivpic)) 
    {
      Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show(); 
      // new RegisterAsyntaskNew().execute(); 
    } 
  else 
    {
     Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show(); 
     // new RegisterAsyntask().execute(); 
    }

6 个答案:

答案 0 :(得分:57)

您好请尝试以下

if (regProfile.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ivpic).getConstantState()) 
{
  Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show(); 
  // new RegisterAsyntaskNew().execute(); 
} 
else 
{
 Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show(); 
 // new RegisterAsyntask().execute(); 
}

请使用.getConstantState()进行比较

访问

http://developer.android.com/reference/android/graphics/drawable/Drawable.html

http://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState.html

修改

.getResources().getDrawable(imageResource)

在API21中已弃用,因此我更改了Jitesh Upadhyay的答案。

以下是代码:

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static boolean checkImageResource(Context ctx, ImageView imageView,
        int imageResource) {
    boolean result = false;

    if (ctx != null && imageView != null && imageView.getDrawable() != null) {
        ConstantState constantState;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            constantState = ctx.getResources()
                    .getDrawable(imageResource, ctx.getTheme())
                    .getConstantState();
        } else {
            constantState = ctx.getResources().getDrawable(imageResource)
                    .getConstantState();
        }

        if (imageView.getDrawable().getConstantState() == constantState) {
            result = true;
        }
    }

    return result;
}

答案 1 :(得分:16)

您可以执行以下操作,

根据您的要求,通过xml或动态设置tag

通过xml,

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageview1" 
        android:background="@drawable/bg"
        android:tag="bg"/>

动态,

ImageView imageView=(ImageView)findViewById(R.id.imageview1);
imageView.setTag("bg");

使用imageView.getTag()检索图像信息。

String backgroundImageName = String.valueOf(imageView.getTag()); 

编辑:

如果您经常更改ImageView背景,

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

现在你可以进行检查了。

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

答案 2 :(得分:0)

我已经在这里回答了类似的话题:Get the ID of a drawable in ImageView。 该方法基于在自定义LayoutInflater中使用指定的资源ID标记视图。整个过程由一个简单的库TagView自动完成。

因此,您可以仅通过ID来比较两个drawable:

TagViewUtils.getTag(regProfile, ViewTag.IMAGEVIEW_SRC.id) == R.drawable.ivpic

答案 3 :(得分:0)

我面临同样的问题,但我在kotlin Android中解决了

override fun onClick(v: View?) {

    when (v!!.getId()) {

        R.id.exo_fullscreen_button -> {
            if (exo_fullscreen_icon.drawable.constantState == resources.getDrawable( R.drawable.exo_controls_fullscreen_enter).constantState) {
                Toast.makeText(activity, "Correct Image ", Toast.LENGTH_LONG).show();

            }else{
                Toast.makeText(activity, "wrong image", Toast.LENGTH_LONG).show();

            }
        }
    }

}

答案 4 :(得分:0)

你可以这样做。首先你需要像这样设置imageview的tag属性

 android:tag="ic_grade_grey_24dp"

if (viewHolder.imgfav.getTag().equals("ic_grade_grey_24dp"))  // here "bg" is the tag that you set previously
            {
                Toast.makeText(mContext, "ic_grade_black_24dp", Toast.LENGTH_LONG).show();
                // new RegisterAsyntaskNew().execute();
                viewHolder.imgfav.setImageResource(R.drawable.ic_grade_black_24dp);
                viewHolder.imgfav.setTag("ic_grade_black_24dp");
            }
            else
            {
                Toast.makeText(mContext, "Second", Toast.LENGTH_LONG).show();
                viewHolder.imgfav.setImageResource(R.drawable.ic_grade_grey_24dp);
                viewHolder.imgfav.setTag("ic_grade_grey_24dp");
            }

答案 5 :(得分:0)

科特林中的相同解决方案:

if (regProfile.drawable.constantState == ContextCompat.getDrawable(this, R.drawable.ivpic).constantState) 
   { 
       Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show(); 
       // new RegisterAsyntaskNew().execute(); 
   } 
else 
   {
       Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show(); 
       // new RegisterAsyntask().execute(); 
   }