Android的绘图资源:有什么办法整型标识以某种方式连接到R绘制的ID? [英] Android drawables : Is there any way to somehow link the int IDs to the R drawable IDs?

查看:217
本文介绍了Android的绘图资源:有什么办法整型标识以某种方式连接到R绘制的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 718可绘绘-MDPI 绘制,华电国际绘-xhdpi 文件夹,他们的名字都1.png,2.png ......到718.png。 (他们实际上是神奇宝贝精灵)

I have 718 drawables in the drawable-mdpi, drawable-hdpiand drawable-xhdpi folders, whose names are 1.png, 2.png... to 718.png. (They are actually Pokémon sprites)

因此​​,根据不同的神奇宝贝,我想要加载其中一人表现出来。但是,我不能用数字直接识别第r绘制ID。

So, depending on the Pokémon, I want to load one of them to show it. However, I cannot use the number to directly identify the R drawable ID.

有什么办法(除了一个718例开关或的Hashmap&LT的;整数,整数GT; )?整型标识以某种方式连接到R绘制的ID

Is there any way (besides of a 718-case switch or a Hashmap<Integer,Integer>) to somehow link the int IDs to the R drawable IDs?

推荐答案

您可以尝试:

String sprite = "drawable/"+number; 
int imageResource = getResources().getIdentifier(sprite, null, getPackageName());

绘制是图像的文件夹。在数量是你想要的图像数量。例如1.png

The drawable is the folder where the image is in. The number is the number of the image you want. e.g. 1.png

这篇关于Android的绘图资源:有什么办法整型标识以某种方式连接到R绘制的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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