1
私は基本的なゲームループを以下に示します。しかし、私がキーボードをスライドさせると、ループはより速く実行されます。私は遅延時間が変わらないので、これを理解できません。前 - 後は常に1または2ミリ秒です。なぜこれが起こっているのか誰も説明できますか? -ThanksKeyBoard Slide Out/Configs後のAndroidループの高速化
public abstract class GameLayout extends LinearLayout {
// App context
private Context context;
// Timer period
private long mPeriod =75;
// ---------[ GAME LOOP ]-----------------
protected Handler gameHandler;// = new Handler();
private Runnable gameRunnable = new Runnable() {
public void run() {
long before = SystemClock.uptimeMillis();
gameLoop();
postInvalidate(); // causes UI to be redrawn
long after = SystemClock.uptimeMillis();
long delay = mPeriod-(after-before);
if (gameHandler != null) {
if(delay >0)
gameHandler.postDelayed(this, delay);
else gameHandler.postDelayed(this, 0);
}
}
};
protected void startUpdateTimer() {
// System.out.println("start timer");
gameHandler = new Handler();
gameHandler.post(gameRunnable);
}
enter code here
protected void stopUpdateTimer() {
// System.out.println("stop timer");
gameHandler.removeCallbacks(gameRunnable);
gameHandler = null;
}
public void setUpdatePeriod(long updateDelay) {
mPeriod = updateDelay;
}
// -----------------------------------------------------
public GameLayout(Context c) {
super(c);
context = c;
}
public GameLayout(Context c, AttributeSet attrs) {
super(c, attrs);
context = c;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
try {
// Init game
initialize();
/**
* start update task. Which will fire onDraw in the future
*/
startUpdateTimer();
} catch (Exception e) {
// bug
e.printStackTrace();
}