目前我正在 Android 应用程序中绘制 PNG 图像,如下所示:
ImageView image = new ImageView(context);
image.setImageDrawable(context.getResources().getDrawable(R.drawable.testimage))
如果我在数据库中有图像名称列表,有没有办法使用图像名称设置上面的可绘制对象?我已经有了遍历数据库的代码,我只是想根据从这里获取的值绘制图像。
例如数据库的一条记录:
ID: Name: ImageName:
- Test testimage
因此,当我阅读此记录时,我有一个值为“testimage”的字符串,然后我想将图像可绘制设置为R.drawable.testimage
.
我想到的一种方法是这样的:
int image = R.drawable.blank; // blank image
// testimage.png is the image name from the database
if(imageName.toString().equals("testimage.png"))
image = R.drawable.testimage;
else if(imageName.toString().equals("another.png"))
image = R.drawable.another;
else if(imageName.toString().equals("etc.png"))
image = R.drawable.etc;
然而这不是很有效!
Thanks
有一种方法可以做到这一点,您可以使用 Resources.getIdentifier() 通过字符串检索资源 IDhttp://developer.android.com/reference/android/content/res/Resources.html http://developer.android.com/reference/android/content/res/Resources.html
就像是:
int resourceId = Activity.getResources().getIdentifier("testimage", "drawable", "your.package.name");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)