21

I want to take a byte and append it to a resource ID to be able to get the image that corresponds to that numbered deck in the game. It was easy to with paths on other devices, but with the Resource ID's I am unsure how I could go about do this.

Here's what I have now:

switch(GameSettings.gameDeck)
    {
    case 1:
        deckImage.setBackgroundResource(R.drawable.deck1);
        break;
    case 2:
        deckImage.setBackgroundResource(R.drawable.deck2);
        break;
    case 3:
        deckImage.setBackgroundResource(R.drawable.deck3);
        break;
    case 4:
        deckImage.setBackgroundResource(R.drawable.deck4);
        break;
    }

In my Blackberry version of this, I simply had:

deckImage.setBitmap(Bitmap.getBitmapResource("/path/deck" + GameSettings.gameDeck + ".png"));

Is there a way to accomplish something similar using Resource IDs on Android?

Matt Swanson
  • 1,194
  • 2
  • 11
  • 28

2 Answers2

48

Use getResources().getIdentifier() from your Context (e.g., Activity), but please cache the result if you will use it more than once. getIdentifier() is implemented on Resources.

For example:

int drawableId=getResources().getIdentifier("foo"+index, "drawable", getPackageName());

would return the value of R.drawable.fooN, where N is the number given by index.

For more, see this and this and this.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 6
    To improve on this answer: Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.myImageName); – RockMeetHardplace Dec 03 '11 at 17:23
  • How would you recommend caching, [LruCache](http://developer.android.com/reference/android/util/LruCache.html)? – theblang Jul 02 '14 at 16:06
  • @mattblang: IDs are only ints, and you shouldn't have millions of IDs. Hence, plain data members (or even local variables outside of loops) should suffice in most cases. – CommonsWare Jul 02 '14 at 16:18
  • @CommonsWare Sorry, I meant the drawables. – theblang Jul 02 '14 at 16:19
  • @mattblang: This question is about resource IDs, not drawables. For bitmaps, ideally you should be reusing those `Bitmap` objects via `inBitmap` in an object pool, due to Dalvik's non-compacting garbage collector. If you cannot do this, then a `LruCache` or something along those lines is a fine choice. Just make sure that if you are using an image processing library (e.g., Picasso) that you aren't redundantly caching things. – CommonsWare Jul 02 '14 at 16:22
0

In your case, the image drawable name are same at begining charecters and the different are the numbers. so when your app is complied. The generated ids for your drawable name are a sequence of consecutive increments. These are ids that generated when I named my drawable as same as your sample code

int drawable deck1 0x7f07007a //convert hex to decimal = 2131165306
int drawable deck2 0x7f07007b //convert hex to decimal = 2131165307
int drawable deck3 0x7f07007c //convert hex to decimal = 2131165308
int drawable deck4 0x7f07007d //convert hex to decimal = 2131165309

You can check the R.txt file which generated by android studio.

So the simple solution is:

 int deckId = R.drawable.deck1 + GameSettings.gameDeck - 1
 deckImage.setImageResource(deckId)
Trung Đoan
  • 643
  • 7
  • 18