Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android xml中检查哪个当前图像资源附加到ImageView?_Android_Xml_Android Imageview - Fatal编程技术网

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

如何在android xml中检查哪个当前图像资源附加到ImageView?,android,xml,android-imageview,Android,Xml,Android Imageview,我想检查哪个图像资源附加到xml中的ImageView,我可以检查哪个图像资源附加到图像视图,但我的要求是如何检查ImageView是否具有与我设置到xml中的相同的资源,基于此,我需要执行一些操作。代码总是执行其他部分。下面是我的代码 if (regProfile.getDrawable() == getResources().getDrawable( R.drawable.ivpic)) { Toast.makeText(_con, "Image is ivPic"

我想检查哪个图像资源附加到xml中的
ImageView
,我可以检查哪个图像资源附加到图像视图,但我的要求是如何检查
ImageView
是否具有与我设置到xml中的相同的资源,基于此,我需要执行一些操作。代码总是执行其他部分。下面是我的代码

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

你可以做如下的事情

通过xml或根据需要动态设置
标记

通过xml

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageview1" 
        android:background="@drawable/bg"
        android:tag="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(); 
    }

您好,请尝试以下方法

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()
进行比较

拜访

编辑:

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

我已经在这里回答了类似的问题:。 该方法基于在自定义
LayoutInflater
中使用指定的资源id标记视图。整个过程由一个简单的库自动化

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

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

我面临同样的问题,但我在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();

            }
        }
    }

}

你可以这样做。首先,您需要像这样设置imageview的标记属性

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

kotlin中的相同溶液:

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

感谢您的回复,但当我更改imageview的图像时,则应执行其他部分,这就是为什么我试图获取哪个资源连接到imageview。这是我使用的逻辑。谢谢你。。。setTag和getTag相当棘手。。。但是是的,很好。谢谢@springbreaker答案已更新请查看请告知我的答案是否适合您,因为我用我的代码进行了检查,它工作正常!!我只是为这个场景工作,花了一些时间在上面,我想我已经成功了@我似乎无法访问上下文?这也是不推荐的吗?@Rekt您可能可以通过android docThanks Jitesh,使用.getConstantState(),完成任务。感谢Nlkolay爵士,您的更新代码完成了我的工作。最受欢迎的@Reena和SimpleProgrammerhi,为什么这个方法总是返回false,即使图像src图像与imageResource参数相同?(R.mipmap.testImage)您可以使用ResourcesCompat,这消除了生成版本检查的需要,因为它是由getDrawable()函数完成的。如何在RecyclerView适配器中使用它?您从哪里获得
资源
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(); 
   }