我正在爲Android編寫遊戲,並且在GC運行時每隔幾秒就會有明顯的暫停。我已經將分配減少到最低限度,剩餘的分配(根據分配跟蹤器)由Bitmap.createBitmap(..)
,onTouchEvent
和onKeyEvent
生成。 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);
}
遊戲是類似蛇的東西,你是一隻鳥和每個蠕蟲你吃了你會得到一隻更小的小鳥休耕你。第二個位圖用於以下較小的鳥。
是的,你需要預先分配和重用兩者。如果您想獲得建議,可能需要顯示當前的實施情況。 – mibollma
@mibollma:完成。那麼,我如何預先分配和重用呢? – SBoss