カスタムビューをサムコントロールとして使用してゲームを作成していました。。。これが私がしたことです
float x = 0, y = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
// handle touch events with
switch( event.getActionMasked() ) {
case MotionEvent.ACTION_DOWN :
if(cont)
{
// remove any previous callbacks
removeCallbacks(contin);
// post new runnable
postDelayed(contin, 10);
}
invalidate();
return true;
case MotionEvent.ACTION_MOVE :
if(!cont && thumbing != null)
{
// do non-continuous operations here
}
invalidate();
return true;
case MotionEvent.ACTION_UP :
// set runnable condition to false
x = 0;
// remove the callbacks to the thread
removeCallbacks(contin);
invalidate();
return true;
default :
return super.onTouchEvent(event);
}
}
public boolean cont = false;
// sets input to continuous
public void set_continuous(boolean b) { cont = b; }
public Runnable contin = new Runnable()
{
@Override
public void run() {
if(x != 0)
{
// do continuous operations here
postDelayed(this, 10);
}
}
};
ただし、このビューを呼び出すメインアクティビティで、次のようにonPauseメソッドを介してコールバックを手動で削除するようにしてください。
@Override
protected void onPause() {
if(left.cont) left.removeCallbacks(left.contin);
if(right.cont) right.removeCallbacks(left.contin);
super.onPause();
}
そうすれば、一時停止して戻ってきた場合、タッチイベントは2回処理されず、ビューはスレッドのオーバーヘッドから解放されます。
**ハードウェアアクセラレーションを使用してSamsungGalaxyS3でテスト済み**