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.

3
  • possible duplicate of Android, getting resource ID from string? Commented Jan 15, 2015 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. Commented Jan 15, 2015 at 13:37
  • thank you icke, thats probably the way I have to do it. I'll give it a try! Commented Jan 15, 2015 at 13:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok, this works for getting the resource from a string.
Click the tick icon to the left of the answer to accept it if it solves your problem.
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.
For that, use an ArrayList using array_list.add(i, bitmap). Later, you retrieve the bitmap suing the index array_list.get(i)
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 :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.