Android的动态显示图像 [英] Android display images dynamically

查看:109
本文介绍了Android的动态显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆2.JPG ......和56.jpg命名的图像,1.JPG的。我也有在其上从previous列表活性选择的项目中的可变依赖性的_id值。我有以下的code来显示图像。

I have a bunch of images named, 1.jpg, 2.jpg… and 56.jpg. I also have an _id value in a variable dependent on which item was selected from the previous list activity. I have the following code to display an image.

ImageView image = (ImageView) findViewById(R.id.headerimage);
image.setImageResource(R.drawable.image1);

我想要做的是动态显示与_id值对应的图像,例如..这样的事情?

What I would like to do is dynamically display the image that corresponds with the _id value, e.g.. something like this?

ImageView image = (ImageView) findViewById(R.id.headerimage);
image.setImageResource(R.drawable.myID + ".jpg");

我该怎么办呢?

干杯,

麦克。

推荐答案

是的,你可以做到这一点。

Yes, you can do this.

只需使用<一个href=\"http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29\"相对=nofollow> Resouces.getIdentifier()。请注意,这个实现是慢,使用的ID和经常检查RESOURCEID不为0 (这将意味着你要的资源不会退出。

Just use Resouces.getIdentifier(). Be aware that this implementation is slower that using the id and always check if the resourceId is not 0 (that would mean the resource you asked for doesn't exit.

例如:

 View view = //...
 int number = //...      
 int resId = getIdentifier("files_"+number, "drawable", "com.my.project.package");
 view.setBackgroundResource(resId);

这篇关于Android的动态显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆