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

前端 未结 6 688
说谎
说谎 2020-12-05 01:48

I want to check which image resource is attached to ImageView in xml, I am able to check that which image resource is attached to image view but my requirement

相关标签:
6条回答
  • 2020-12-05 02:23

    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.

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

    TagViewUtils.getTag(regProfile, ViewTag.IMAGEVIEW_SRC.id) == R.drawable.ivpic
    
    0 讨论(0)
  • 2020-12-05 02:25

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

     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");
                }
    
    0 讨论(0)
  • Same solution in 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(); 
       }
    
    0 讨论(0)
  • 2020-12-05 02:34

    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

    visit

    http://developer.android.com/reference/android/graphics/drawable/Drawable.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.

    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;
    }
    
    0 讨论(0)
  • 2020-12-05 02:34

    I face same problem but i resolved in 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();
    
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-05 02:35

    You can do something like following,

    Set a tag either through xml or dynamically as per your requirement.

    Through 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.

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

    Edit:

    if you are changing ImageView background frequently then,

    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(); 
        }
    
    0 讨论(0)
提交回复
热议问题