1

To draw on a canvas I need to get the image file from the resources like this:

bmpNode = BitmapFactory.decodeResource(getResources(), R.drawable.node);

After that i can call my canvas to draw:

c.drawBitmap(bmpNode, xPos, yPos, null);

The Problem is that I have a large amount of unique Node-objects with ID's as a String value. I numbered them from "1" to "100". Each node has its own image file in the resources also called "1" - "100". The way I know, I'd had to do a hundred lines of code to get every image:

1 = BitmapFactory.decodeResource(getResources(), R.drawable.1);
2 = BitmapFactory.decodeResource(getResources(), R.drawable.2);
[...]
100 = BitmapFactory.decodeResource(getResources(), R.drawable.100);

Now instead of putting 100 lines of code for each Image I would like to make a loop, witch would function something like the following code:

for (int i=0; i<arrayNodes.length; i++){        //for every Node in the Array
    "i" = BitmapFactory.decodeResource(getResources(), R.drawable."i");
}

I was trying to look into reflection and maps, but I am new to this kind of problem and couldn't apply anything I found. Also I have no idea how to properly search for this problem, because I don't know what the input value for the method "R.drawable" is called.

XikiryoX
  • 1,898
  • 1
  • 12
  • 33
Deckweiss
  • 29
  • 7
  • possible duplicate of [Android, getting resource ID from string?](http://stackoverflow.com/questions/4427608/android-getting-resource-id-from-string) – Hemanth Jan 15 '15 at 13:32
  • I've looked into that, but I have problems applying the solution to name the variables after the file names. Nor do I understand how to solve it in a different way. – Deckweiss Jan 15 '15 at 13:37
  • thank you icke, thats probably the way I have to do it. I'll give it a try! – Deckweiss Jan 15 '15 at 13:48

1 Answers1

1

Get the id like this.

ArrayList<Bitmap> bitmapArrayList = new ArrayList<Bitmap>();
for (int i=0; i<arrayNodes.length; i++){
  bitmapArrayList.add(i, BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(String.valueOf(i), "drawable", package_name)));
}

To retrieve Bitmap:

bitmapArrayList.get(i);

If no such resource exist, getIdentifier() will return 0.

Hemanth
  • 2,717
  • 2
  • 21
  • 29
  • Ok, this works for getting the resource from a string. – Deckweiss Jan 15 '15 at 13:42
  • Click the tick icon to the left of the answer to accept it if it solves your problem. – samgak Jan 15 '15 at 13:45
  • It solves a part of it, because I want to name every one of BitmapFactory.decodeResource() from "1" to "100" so i can have them preloaded. Otherwise I'd have to load and decode all 100 images while drawing, during every canvas tick and that would totally lag. – Deckweiss Jan 15 '15 at 13:46
  • For that, use an _ArrayList_ using `array_list.add(i, bitmap)`. Later, you retrieve the bitmap suing the index `array_list.get(i)` – Hemanth Jan 15 '15 at 13:51
  • Thanks arol_123 ! Someone else suggested using a (hash)-map for that and then deleted his comment :( . I'm not sure what is better performance wise, but I'll research that and go with it :) – Deckweiss Jan 15 '15 at 15:03
  • When you need a structure from which you will be retrieving items randomly - use a _HashMap_. When you will be retrieving items in order (e.g. using a for loop) - use an _ArrayList_. I think it would be better to use _ArrayList_ in your case. – Hemanth Jan 15 '15 at 15:10