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

来源:互联网 发布:iphone8爆炸知乎 编辑:程序博客网 时间:2024/06/05 15:20

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

this.img.getTag()

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



imageview drawable id in android

up vote5down votefavorite
3

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.

shareimprove 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 MayaniDec 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

activeoldestvotes
up vote11down voteaccepted

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;  }});
shareimprove 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 
up vote2down vote

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.

0 0