Dynamically get drawables by ID

前端 未结 1 1866
小鲜肉
小鲜肉 2020-11-30 08:36

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

相关标签:
1条回答
  • 2020-11-30 09:38

    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.

    0 讨论(0)
提交回复
热议问题