采用可变绘制资源 [英] Drawable resource using a variable

查看:110
本文介绍了采用可变绘制资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个变量而不是R.drawable.myimage,因为我有一个形象的名称的数据库。我从数据库中获取的名字,我想用这个名字的绘制资源。

 字符串flagimage =R.drawable。 + myHelper.getFlagImage(5);
INT RESOURCEID =的Integer.parseInt(flagimage);
位图标志= BitmapFactory.de codeResource(getResources(),RESOURCEID);
 

解决方案

您可以使用资源的名称,比如

则getIdentifier(字符串名称,字符串DEFTYPE,字符串defPackage);

  getResources()则getIdentifier(我们,绘制,com.app)。
 

上面的函数将返回相同R.drawable.us一个整数值。

这是你如何访问资源名。

I want to use a variable instead of R.drawable.myimage because I have a database with the image's names. I get the name from the database and I want to use the drawable resource with this name.

String flagimage = "R.drawable." + myHelper.getFlagImage(5);
int resourceId = Integer.parseInt(flagimage);
Bitmap flag = BitmapFactory.decodeResource(getResources(), resourceId);

解决方案

You can use the name of a resource like

getIdentifier (String name, String defType, String defPackage);

getResources().getIdentifier("us","drawable","com.app");

The above function will return an integer value same as R.drawable.us.

This is how you access with resource names.

这篇关于采用可变绘制资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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