Android, you want to link the bitmaps in the array using strings, programmatically

I am looking for a good programmatic way that I can use to refer to images in my bitmap [].

I am currently declaring 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;

Then I refer to them as follows:

mBmp[someimage] ...

However, this is inefficient, and I would like to refer to them (preferably) according to their file name (minus the extension) or another unique identifier that can be programmatically determined.

The reason for this is that:

  • number of images arbitrarily
  • file names are arbitrary
  • I want to automate the process as a template.
+3
source share
2 answers

? 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");
+2

, HashMap. , , . String . - , HashMap - .

-, - :

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);
}
+3

Source: https://habr.com/ru/post/1789976/


All Articles