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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23357706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:02:51  来源:igfitidea点击:

How to check which current image resource is attached to ImageView in android xml?

androidxmlandroid-imageview

提问by Reena

I want to check which image resource is attached to ImageViewin xml, I am able to check that which image resource is attached to image view but my requirement is to how to check that the ImageViewhas the same resource which I have set into xml or not, based on that I need to perform some actions.Code always executes else part. Following is my code,

我想检查ImageViewxml 中附加了哪个图像资源,我可以检查哪个图像资源附加到图像视图,但我的要求是如何检查是否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(); 
    }

回答by Jitesh Upadhyay

Hi please have a try with this as follows

嗨,请按如下方式尝试

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

please use .getConstantState()to compare

.getConstantState()用来比较

visit

访问

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

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

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

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

EDIT:

编辑:

.getResources().getDrawable(imageResource)

Is deprecated in API21, so I changed Jitesh Upadhyay's answer.

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

Here is the code:

这是代码:

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

回答by Spring Breaker

You can do something like following,

您可以执行以下操作,

Set a tageither through xml or dynamically as per your requirement.

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

Through xml,

通过xml,

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

Dynamically,

动态地,

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

Use imageView.getTag()to retrieve image information.

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

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

Edit:

编辑:

if you are changing ImageViewbackground frequently then,

如果你ImageView经常更换背景,那么

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

Now you can make your check.

现在您可以进行检查了。

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

回答by Noelia

Same solution in kotlin:

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

回答by Bogdan Kornev

I already answered on the similar topic here: Get the ID of a drawable in ImageView. The approach is based on tagging a view with a specified resource id in the custom LayoutInflater. Whole process is automated by a simple library TagView.

我已经在这里回答了类似的主题:Get the ID of a drawable in ImageView。该方法基于在自定义LayoutInflater. 整个过程由一个简单的库TagView自动化。

As a result, you can compare two drawables just by their ids:

因此,您可以仅通过 id 比较两个可绘制对象:

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

回答by Prince Kumar

I face same problem but i resolved in kotlin Android

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

            }
        }
    }

}

回答by Syed Danish Haider

u can do like this. first u need to set tag property of imageview like this

你可以这样做。首先你需要像这样设置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");
            }