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

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

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.

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值