批量获取Android上的许多位图资源 [英] Batch getting many bitmap resources on Android
问题描述
我有很长的系列显卡 - icon1_0.png,icon1_1.png,icon1_2.png ...,icon12_0.png,icon12_1.png,icon12_2.png - 我想他们包我Android应用程序。理想情况下,我想我应该能够加载它们的资源,但资源的ID被设置为java的标识符。当然,java的标识符不能在运行时被组装。我要问R.drawable.icon12_00所以我不能建立一个循环
I have a long series of graphics -- icon1_0.png, icon1_1.png, icon1_2.png..., icon12_0.png, icon12_1.png, icon12_2.png -- and I'd like to package them with my android application. Ideally I think I should be able to load them as resources but the resource id's are set up as java identifiers. Of course, java identifiers can't be assembled at runtime. I have to ask for R.drawable.icon12_00 so I cannot set up a loop
for(int icon=0;icon<12;icon++)
for(int frame=0;frame<3;frame++)
//syntax error obviously
BitmapFactory.decodeResource(getResources(), R.drawable."icon" + icon + "_" + frame + ".png");
那么,有没有办法用自己的名字来获得资源呢?更重要的是,是否有资源系统外的正规途径打包的数据文件到一个Android应用程序包,这样我可以在他们获得?
So is there any way to get resources by their names? Better yet, is there a canonical way outside the resource system to pack data files into an android application package so that I can get at them?
我在想反映,但似乎并不像正确的解决方案给我。
I'm thinking about reflection but that doesn't seem like the right solution to me.
推荐答案
使用 getResources()。则getIdentifier()
从上下文
(例如,活动
),但请缓存结果,如果你会用它不止一次。 则getIdentifier()
是在<一个实施href="http://developer.android.com/reference/android/content/res/Resources.html"><$c$c>Resources$c$c>.
Use getResources().getIdentifier()
from your Context
(e.g., Activity
), but please cache the result if you will use it more than once. getIdentifier()
is implemented on Resources
.
这篇关于批量获取Android上的许多位图资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!