Android,想要以编程方式使用字符串引用数组中的位图
我正在寻找一种很好的编程方式,可以用来引用位图[]中的图像。
目前,我在类中声明了一堆整数,
Bitmap[] mBmp = new Bitmap[6]
int image1 = 0, image2 = 1, image3 = 2, someimage = 3, otherimage = 4, yetanoimage = 5;
然后按如下方式引用它们:
mBmp[someimage] ...
但是,这是低效的,我想根据它们的文件名(减去扩展名)或其他一些独特的名称来引用它们(最好)标识符比可以通过编程确定。
原因是:
- 图像数量是任意的
- 文件名是任意的
- 我想将过程作为模板自动化。
I'm looking for a nice programatic way I can use to refer to the images in my Bitmap[].
At the moment, I declare a bunch of integers in my class
Bitmap[] mBmp = new Bitmap[6]
int image1 = 0, image2 = 1, image3 = 2, someimage = 3, otherimage = 4, yetanoimage = 5;
I then refer to them as follows:
mBmp[someimage] ...
However, this is inefficient and I would like to refer to them (preferrably) according to their filename (minus the extension) or some other unique identifier than can be programatically determined.
The reason for this is that:
- number of images is arbitrary
- file names are arbitrary
- I want to automate the process as a template.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的建议是将图像放入 HashMap 中。 Map 类似于 Array,但有一个对象作为键。在这种情况下,我建议您使用 String 对象作为键。 Map是集合类型,HashMap是Map的实现。
要创建哈希图,您需要执行以下操作:
要插入图像:
然后可以像这样检索位图:
您可以通过执行以下操作来迭代位图:
My recommendation would be to put your images into a HashMap. A Map is like an Array but with an object that works as the key. In this case, I'm suggesting you use a String object as the key. Map is the collection type, and HashMap is an implementation of Map.
To create a hashmap you'd do something like:
To Insert an image:
Retrieving a bitmap can then be done like so:
You can iterate over the bitmaps by doing:
必须使用数组吗?为什么不使用 Hashtable 来存储你的位图?这样您就可以使用它们的(唯一)文件名作为标识符来获取它们。
根据示例,插入位图:
并获取位图:
Do you have to use an array? Why don't you use an Hashtable to store your bitmaps? That way you could fetch them using their (unique) filename as their identifier.
As per the example, inserting bitmaps:
And fetching bitmaps: