I’m writing a game for Android and I’m having noticeable pauses every few seconds when the GC runs. I’ve reduced the allocations to a minimum and the only remaining allocations (according to Allocation Tracker) are produced by Bitmap.createBitmap(..), an onTouchEvent and an onKeyEvent. Bitmap.createBitmap(..) is called twice per Frame, and cannot be removed.
Now to my question: Is there an alternative way to read the events and another way to create bitmaps? Or a way to pre-allocate the memory needed for the operations and a way of telling them to use that?
InputHandler class (contains listeners):
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;
}
};
}
}
Bitmap-creating part
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);
}
The game is something similar to snake, you’re a bird and for each worm you eat you get a smaller bird fallowing you. The second bitmap is for the following, smaller bird.
For reusable input events you might want to take a look at this implementation: http://code.google.com/p/libgdx/source/browse/trunk/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidInput.java
Regarding bitmaps… maybe you should make some sprites the same size so you can reuse your bitmap.