动态获取可绘制由ID

编程入门 行业动态 更新时间:2024-10-09 15:14:57
本文介绍了动态获取可绘制由ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想借此字节并将其附加到一个资源ID,以便能够获得对应于在游戏中号甲板上的图像。这是很容易与其它设备的路径,但随着资源的ID我不确定我怎么可能去做到这一点。

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.

下面是我现在有:

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"));

有没有办法来完成对Android的类似的东西使用资源的ID?

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

推荐答案

使用 getResources()。则getIdentifier()从上下文(例如,活动),但请缓存结果,如果你会用它不止一次。 则getIdentifier()在资源实施。

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.

有关更多信息,请参阅this和this和this.

For more, see this and this and this.

更多推荐

动态获取可绘制由ID

本文发布于:2023-11-22 00:31:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!