1

好的,我的问题是这个

        switch (charType) {
    case Mush:
        switch (charColor) {
        case Blue:
            switch (charDirec) {
            case Left:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.l_blue_m);
                break;
            case Right:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.r_blue_m);
                break;
            case Bounce:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.b_blue_m);
                break;
            case Walk:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.w_blue_m);
                break;
            }
            break;
        case Red:
            switch (charDirec) {
            case Left:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.l_red_m);
                break;
            case Right:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.r_red_m);
                break;
            case Bounce:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.b_red_m);
                break;
            case Walk:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.w_red_m);
                break;
            }
        case Tan:
            switch (charDirec) {
            case Left:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.l_tan_m);
                break;
            case Right:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.r_tan_m);
                break;
            case Bounce:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.b_tan_m);
                break;
            case Walk:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.w_tan_m);
                break;
            }
            break;
        }
        break;
    case Slime:
        switch (charColor) {
        case Blue:
            switch (charDirec) {
            case Left:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.l_blue_s);
                break;
            case Right:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.r_blue_s);
                break;
            case Bounce:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.b_blue_s);
                break;
            case Walk:
                bitmap = (Bitmap) BitmapFactory.decodeResource(
                        mainContext.getResources(), R.drawable.w_blue_s);
                break;
            }
            break;
        }
        break;
    }

有什么更好的方法来处理这个问题?或者这是正确的?

4

2 回答 2

3

您可以首先将您的方法拆分为两个更简单的方法。一种方法应该找到正确的资源 ID,另一种方法实际获取Bitmap

int resourceId = getResourceId(charType, charColor, charDirec);
bitmap = (Bitmap)BitmapFactory.decodeResource(
    mainContext.getResources(), resourceId);

这个简单的更改已经删除了很多重复的代码。

在您的getResourceId方法中,您可能需要考虑使用映射来查找资源 id 而不是嵌套开关。

Map<CharKey, Integer> map = new HashMap<CharKey, Integer>();

int getResourceId(charType, charColor, charDirec)
{
    CharKey key = new CharKey(charType, charColor, charDirec);
    return map.get(key).intValue();
}

剩下的就是在程序启动时填充字典。

于 2012-08-23T23:34:44.507 回答
1

我假设您正在使用 charType、charColor、charDirec 的枚举 - 如果您更改为 ints,您可以简单地创建一个 3D 数组,并将它们用作索引。在应用程序启动时预填充一次,这看起来像

int[][][] resources;

public void initResources(int numTypes, int numColors, int numDirections) {
  resources = new int[numTypes][numColors][numDirections];
  resources[MUSH][BLUE][LEFT] = R.drawable.l_blue_m;
  resources[MUSH][BLUE][RIGHT] = R.drawable.r_blue_m;
  // etc etc
}

然后您可以稍后访问 ID,如下所示:

int resourceID = resources[charType][charColor][charDirec];
bitmap = (Bitmap) BitmapFactory.decodeResource(
                    mainContext.getResources(), resourceID);

This way your application doesn't need to navigate a switch statement every time it wants to look up a bitmap ID, just access the relevant index in a multidimensional array.

于 2012-08-23T23:54:54.530 回答