getImageResource() Android. Is this possible?

2019-04-21 16:57发布

问题:

I have set an image for an ImageView using the setImageResource(R.drawable.icon1).

Now my requirement is to find out what is the image that is set for an ImageView and do some processing.

Like

if (imageView.getImageResource() == R.drawable.icon1) {
  //do some processing
else if (imageView.getImageResource() == R.drawable.icon2) {
  //do somethign else
else 
 //display

So I would like to know if there exists a method(AFAIK, it doesn't) and if it doesn't how to keep a track of what resources have been set for an ImageView.

Thanks. Sana.

回答1:

You're assuming that because you put an integer in, you are able to get an integer back out, and that's not how setImageResource() works. See ImageView#setImageResource(). This is just a convenience method for you: what Android is doing behind the scenes, is looking up the Drawable resource (in most cases, it's a BitmapDrawable, but it could be any type), and then applying that resource to the ImageView as a Bitmap object (i.e., image data only -- it does not have any idea what its original "resource id" was previously).

Your best solution is going to be keeping track of the last resource id you used:

imageView.setImageResource(R.drawable.image1);
this.mLastResourceId = R.drawable.image1;
// ...

// Later, when checking the resource:
if (this.mLastResourceId == R.drawable.image1) {
    // Do something
}


回答2:

You should be able to use the tag property as follows:

imageView.setImageResource(R.drawable.icon1);
imageView.setTag(Integer.valueOf(R.drawable.icon1));

// later

int drawable = (Integer) imageView.getTag();

switch(drawable) {

    case R.drawable.icon1:
         // do_something....
         break;

    case R.drawable.icon2:
         // do_something....
         break;

    ...
}


回答3:

Another alternative, if possible, would be to subclass the ImageView and store the integer in an overridden setImageResource().

public class MyImageView extends ImageView  {
  int rememberId = -1;
  @override void setImageResource(int resId){
    rememberId = resId;
  }  
  int getMyResId(){
    return rememberId;
  }
}


回答4:

package com.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageButton;

/**
 * custom star type imagebutton that can be obtain the background image resource
 * id.
 * 
 * </p> your layout xml resource might be like the following code: <br>
 * <view class="com.widget.StarButton" <br>
 *      android:id="@+id/starButton" <br>
 *      android:layout_width="wrap_content" <br>
 *      android:layout_height="wrap_content" <br>
 *      android:background="@android:color/background_light" <br>
 *      android:paddingTop="10dp" <br>
 *      android:src="@drawable/star" /> <br>
 * 
 * @author Jeffen
 * 
 */
public class StarButton extends ImageButton {
private int mLastResourceId = -1;

public StarButton(Context context) {
    super(context);
}

public StarButton(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.imageButtonStyle);
}

public StarButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setFocusable(true);
}

/**
 * set image resource and sync image resource id.
 */
@Override
public void setImageResource(int resId) {
    super.setImageResource(resId);

    setImageResourceId(resId);
}

public int getImageResourceId() {
    return mLastResourceId;
}

public void setImageResourceId(int resId) {
    mLastResourceId = resId;
}

}


回答5:

You should be able to use Bundle and set whatever properties you need.

Intent i = new Intent();      
Bundle extras = new Bundle();
      i.putExtra("prop", "value");