安卓 imageview资源图片ID获取方法

时间:2021-11-11 22:43:34

img.setImageResource(R.drawable.a2);
img.setTag(R.drawable.a2);

this.img.getTag()

来自http://*.com/questions/4526585/imageview-drawable-id-in-android



imageview drawable id in android

i had taken one imageview and set a drawable on it. Now i need to get id of drawable onclick event of imageview dynamicaly. How can i get it? anyone who already messes with it and have a solution pls help me.

share improve this question
 
 
Paste your code, please. –   Cristian  Dec 24 '10 at 13:53
 
what have you tried so far ? have you tried by yourself, or directly run here for the solution? –   Paresh Mayani Dec 24 '10 at 15:31
 
imgtopcolor=(ImageView)findViewById(R.id.topcolor); imgtopcolor.setImageResource(R.drawable.dr); now ontouchevent of imgtopcolor i want to need drawable id .bcoz i am setting different drawable each time and want to compare the drawable with other. –   chirag shah  Dec 25 '10 at 3:06

4 Answers

up vote 11 down vote accepted

I think if I understand correctly this is what you are doing.

ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
  public void onClick(View view) {
   ImageView imageView = (ImageView) view;
   assert(R.id.someImage == imageView.getId());
   switch(getDrawableId(imageView)) {
    case R.drawable.foo:
     imageView.setDrawableResource(R.drawable.bar);
     break;
    case R.drawable.bar:
    default:
     imageView.setDrawableResource(R.drawable.foo);

     break;
  }
});

Right? So that function getDrawableId() doesn't exist. You can't get a the id that a drawable was instantiated from because the id is just a reference to the location of data on the device on how to construct a drawable. Once the drawable is constructed it doesn't have a way to get back the resourceId that was used to create it. But you could make it work something like this:

ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
  public void onClick(View view) {
   ImageView imageView = (ImageView) view;
   assert(R.id.someImage == imageView.getId());
   Integer integer = (Integer) imageView.getTag();
   integer = integer == null ? 0 : integer;
   switch(integer) {
    case R.drawable.foo:
     imageView.setDrawableResource(R.drawable.bar);
     imageView.setTag(R.drawable.bar);
     break;
    case R.drawable.bar:
    default:
     imageView.setDrawableResource(R.drawable.foo);
     imageView.setTag(R.drawable.foo);
     break;
  }
});
share improve this answer
 
 
Thanks for reply.but i think there is no such method like getDrawableID(). –   chirag shah  Dec 25 '10 at 5:21
 
That's what I said. Please read the whole solution. –   Greg Giacovelli  Dec 25 '10 at 5:47
 
Thanks for reply.i think it will work for me –   chirag shah  Dec 27 '10 at 9:29
 
did it work for you? –   Greg Giacovelli  Dec 28 '10 at 8:53
 
ya sure but i have to than convert the tag object into integer to get tag.and my job is done –   chirag shah  Jan 1 '11 at 5:12 

I answered something like this in another question already, but will change it just a little for this one.

Unfortunately, there is no getImageResource() or getDrawableId(). But, I created a simple workaround by using the ImageView tags.

In onCreate():

imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);

imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);

Then, if you like, you can create a simple function to get the drawable id:

private int getDrawableId(ImageView iv) {
    return (Integer) iv.getTag();
}

Too easy.