事件和位图中的预分配

时间:2011-07-05 11:54:26

标签: android garbage-collection allocation

我正在为Android编写游戏,当GC运行时,我每隔几秒就会有明显的暂停。我已将分配减少到最小,剩下的唯一分配(根据分配跟踪器)由Bitmap.createBitmap(..)onTouchEventonKeyEvent生成。每帧调用Bitmap.createBitmap(..)两次,无法删除。

现在问我的问题:是否有另一种方法来读取事件和另一种创建位图的方法?或者预先分配操作所需的内存的方法以及告诉他们使用它的方法?

InputHandler类(包含侦听器):

public class InputHandler {

private ISwarmInput mGame;

// Declare input types
public static final int USE_TOUCH = 0;
public static final int USE_MOTION = 1;
public static final int USE_KEY = 2;
public static final int USE_TOUCH_AND_KEY = 3;

private int mInputType;
public OrientationEventListener mOrientationListener;
public OnTouchListener mTouchListener;
public OnKeyListener mKeyListener;

public InputHandler(Context context, ISwarmInput game) {
    mGame = game;
    mTouchListener = createTouchListener();
    mKeyListener = createKeyListener();
}

public OrientationEventListener createTiltListener(Context context) {
    return new OrientationEventListener(context) {
        @Override
        public void onOrientationChanged(int orientation) {
            if (mInputType == USE_MOTION) {
                mGame.setAngle(orientation);
            }
        }
    };
}

public OnTouchListener createTouchListener() {
    return new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event){
            if (mInputType == USE_TOUCH || mInputType == USE_TOUCH_AND_KEY) {
                if (readPointIn(event.getX(), event.getY())) {
                    mGame.addNewPoint((int) event.getX(),
                            (int) event.getY());
                }
            }
            return true;
        }
    };
}

public OnKeyListener createKeyListener() {
    return new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (mInputType == USE_KEY || mInputType == USE_TOUCH_AND_KEY) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_UP:
                    mGame.setAngle(-90);
                    break;
                case KeyEvent.KEYCODE_DPAD_LEFT:
                    mGame.setAngle(180);
                    break;
                case KeyEvent.KEYCODE_DPAD_DOWN:
                    mGame.setAngle(90);
                    break;
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    mGame.setAngle(0);
                    break;
                }
            }
            return false;
        }
    };
}
}

位图创建部分

public void drawHead(Canvas canv) {
    mat.reset();
    nextFrame();

    setRotationAndFlip(mat);

    this.bmp = Bitmap.createBitmap(SPRITESHEET, Bird.mCurFrame * BIG_W[mUseBird], 0,
            BIG_W[mUseBird], BIG_H[mUseBird], mat, true);
    super.drawPlaceable(canv);

    mat.preScale((float)0.6, (float)0.6);
    this.bmp = Bitmap.createBitmap(SPRITESHEET, Bird.mCurFrame * BIG_W[mUseBird], 0,
            BIG_W[mUseBird], BIG_H[mUseBird], mat, true);
}

游戏类似于蛇,你是一只鸟,你吃的每一只虫子都会让你的小鸟慢慢吞下。第二个位图适用于以下小鸟。

1 个答案:

答案 0 :(得分:0)

对于可重复使用的输入事件,您可能需要查看此实现:http://code.google.com/p/libgdx/source/browse/trunk/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidInput.java

关于位图...也许你应该制作一些相同大小的精灵,这样你就可以重用你的位图。