如何从动态绘制在Android中设置图像? [英] How to set the image from drawable dynamically in android?

查看:124
本文介绍了如何从动态绘制在Android中设置图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我储存我的项目相关的图像绘制文件夹。另外,我存储图像名字字符串变量和动态我想这些图像设置为ImageView的。但是,图像不显示。请帮我在这方面的工作。

I am storing my project related images in drawable folder. Also I am storing the image names in string variable and dynamically I am trying to set those images to the imageview. But the image is not displaying. Please help me in this regard.

我的code:

int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(res);

在上面的codeimagename是包含图像名称的字符串变量。

In the above code "imagename" is the string variable which contains the image name.

在此先感谢

推荐答案

试试这个:

String uri = "@drawable/myresource";  // where myresource.png is the file
                                      // extension removed from the String

int imageResource = getResources().getIdentifier(uri, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

这篇关于如何从动态绘制在Android中设置图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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