Android,想要以编程方式使用字符串引用数组中的位图

发布于 2024-10-15 21:33:42 字数 426 浏览 4 评论 0原文

我正在寻找一种很好的编程方式,可以用来引用位图[]中的图像。

目前,我在类中声明了一堆整数,

Bitmap[] mBmp = new Bitmap[6]    
int image1 = 0, image2 = 1, image3 = 2, someimage = 3, otherimage = 4, yetanoimage = 5;

然后按如下方式引用它们:

mBmp[someimage] ...

但是,这是低效的,我想根据它们的文件名(减去扩展名)或其他一些独特的名称来引用它们(最好)标识符比可以通过编程确定。

原因是:

  1. 图像数量是任意的
  2. 文件名是任意的
  3. 我想将过程作为模板自动化。

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:

  1. number of images is arbitrary
  2. file names are arbitrary
  3. I want to automate the process as a template.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城仅有旧梦在 2024-10-22 21:33:42

我的建议是将图像放入 HashMap 中。 Map 类似于 Array,但有一个对象作为键。在这种情况下,我建议您使用 String 对象作为键。 Map是集合类型,HashMap是Map的实现。

要创建哈希图,您需要执行以下操作:

Map<String, Bitmap> myPictures = new HashMap<String, Bitmap>();

要插入图像:

String fileName = "somefileName";
Bitmap bitmap = Bitmap bitmap = BitmapFactory.decode(fileName)
myPictures.put(fileName, bitmap)

然后可以像这样检索位图:

Bitmap myBitmap = myPictures.get(filename)

您可以通过执行以下操作来迭代位图:

for(Bitmap bitmap : myPictures.values()){
   display(bitmap);
}

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:

Map<String, Bitmap> myPictures = new HashMap<String, Bitmap>();

To Insert an image:

String fileName = "somefileName";
Bitmap bitmap = Bitmap bitmap = BitmapFactory.decode(fileName)
myPictures.put(fileName, bitmap)

Retrieving a bitmap can then be done like so:

Bitmap myBitmap = myPictures.get(filename)

You can iterate over the bitmaps by doing:

for(Bitmap bitmap : myPictures.values()){
   display(bitmap);
}
乖乖 2024-10-22 21:33:42

必须使用数组吗?为什么不使用 Hashtable 来存储你的位图?这样您就可以使用它们的(唯一)文件名作为标识符来获取它们。

根据示例,插入位图:

Hashtable<String, Bitmap> bitmaps = new Hashtable<String, Bitmap>();
numbers.put("one_image", bitmap1);
numbers.put("image_two", bitmap2);
numbers.put("beach_house", bitmap3);

并获取位图:

bitmap = bitmaps.get("beach_house");

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:

Hashtable<String, Bitmap> bitmaps = new Hashtable<String, Bitmap>();
numbers.put("one_image", bitmap1);
numbers.put("image_two", bitmap2);
numbers.put("beach_house", bitmap3);

And fetching bitmaps:

bitmap = bitmaps.get("beach_house");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文