Android 如何获取图片资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26370993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:19:33  来源:igfitidea点击:

How to get Image Resource

androidimagebutton

提问by MOHRE

How should I get current image resource of an imageButton? I need something like this:

我应该如何获取 imageButton 的当前图像资源?我需要这样的东西:

imageButton.getImageResource(); 

Thanks a lot

非常感谢

回答by dimsuz

I think you can't, current API doesn't allow this. But if you really need this, you can do something like this:

我认为你不能,当前的 API 不允许这样做。但是如果你真的需要这个,你可以这样做:

imageButton.setImageResource(R.drawable.your_resource);
imageButton.setTag(R.drawable.your_resource);
//
// somewhere later...
//
Integer resource = (Integer)imageButton.getTag();

回答by Mokhefi Abdelkrim

You can use imageButton.getDrawable()to get the drawable of the ImageButton object and then use setImageDrawable()instead of setImageResource().

您可以使用imageButton.getDrawable()获取 ImageButton 对象的可绘制对象,然后使用setImageDrawable()代替setImageResource()

回答by Mohammad nabil

Use this

用这个

imageButton.setImageResource(R.drawable.your_resource);
imageButton.setTag(R.drawable.your_resource);

Integer resource = (Integer) imageButton.getTag();

if(resource == R.drawable.your_resource){
    // do something
}else {
}