Android:加载多个图像
我最近开始在 android 中开发一个游戏,以前从未使用过它,想知道是否有一种简单的方法可以将一组图像加载到应用程序中。由于我当前的实现基本上涉及
创建一个 int[] 数组,
将每个可绘制对象的值存储到此数组中,(现在必须手动编码,因此如果我添加更多图像,则必须以编程方式添加)
然后遍历数组中的每个项目并调用BitmapFactory 来获取资源。 (不幸的是,我没有携带代码,因为它在家里,而我在工作,但这就是要点)
所以有两个问题,有没有一种方法可以获取可绘制对象,而无需将每个项目手动放入int[] - 即寻找一个文件名前缀然后只加载带有该前缀的资源?
这引出了我的第二个问题,因为我不仅仅是可绘制资源目录中的这些图像,是否有一种方法可以添加额外的组织(文件夹)来更好地管理文件。目前,我的可绘制文件中有大量图像,以及如何引用这些子文件夹/图像。
I have recently started developing a game in android, never used it before and was wondering if there is a simple way of getting a set of images loaded into the application. As my current implementation involves basically
Creating an int[] array,
Storing the values of each drawable into this array, (now this has to be hand coded, so if I add any more images it has to be added programmitically)
Then itterating through each item in the array and calling BitmapFactory to get the resource.
(Unfortunately I don't have the code with me as it is at home and I am at work, but that is the jist)
So 2 questions, is there a way of getting the drawables without having to put in each item manually to the int[] - ie looking for perhaps a file name prefix and then only loading the resource with the prefix?
Which leads me to my second question because I more than just these images in my drawable resource directory, is there a way to add extra organisation (folders) to manage the files better. As currently I have loads of images within the drawable file and how would I reference these sub folders/images.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
资源结构中不能有子文件夹。 Android 根据文件夹布局来确定在什么条件(本地化、不同的屏幕分辨率等)下使用哪些资源。
我不确定您到底为什么要尝试加载一大堆图像,但有一些(较慢的)方法可以让您通过字符串名称查找资源。如果您对图像使用了命名约定,您可以通过 [Resources.getIdentifier()][1] 以这种方式查找它们。然而,在游戏中,性能可能很重要,因此您可能最好采用直接使用 int ID 的手动方法,因为它效率更高。
[1]: http://developer .android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)
You cannot have sub folders within the resources structure. Android depends on the folder layout to determine which resource to use in what condition (localization, different screen resolutions, etc).
I'm not sure why exactly you are trying to load up a whole bunch of images, but there are a couple of (slower) methods that allow you to look up a resource by string name. If you used a naming convention for your images you could look them up that way via [Resources.getIdentifier()][1]. However, in a game performance likely matters, so you are probably better off with a more manual approach using the int IDs directly since it is much more efficient.
[1]: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)
我正在上传大量图像,因为它们将向用户显示不同的项目。它不是一个响应能力至关重要的系统,所以就我想要的而言,它还可以。尽管...
他们建议使用资源 ID,但如果我想稍后添加文件,那么我必须重新编译应用程序以包含额外的文件,这就是它让我烦恼的地方,因为图片与我所拥有的项目相关联一个字符串数组。因此,我可以在不更改代码的情况下将项目添加到数组中,但不能添加图像。
当然有一个功能可以解决这个问题吗?
I am uploading a load of images as they will be shown to user for different items. Its not a system where responsiveness is critical so its okay in terms of what I want. Though...
They suggest using the resource id but if I want to add a file later on then I have to re-compile the app to include the extra file, this is where it bugs me, as the pic gets associated to an item that I have in a string array. So I can add items to the array but not the images without a change of the code.
Surely there is a function to fix this?