2 回答

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
設(shè)置一個(gè)全局布爾值以了解何時(shí)MotionEvent.ACTION_DOWN
收到事件。
mUserTouched = true; // Set to true when MotionEvent.ACTION_DOWN mUserTouched = false; // set to false when MotionEvent.ACTION_UP is received
然后,您可能需要使用一個(gè)Timer
或其他一些循環(huán)程序,每當(dāng)收到 MotionEvent.ACTION_DOWN 時(shí),該循環(huán)程序就會首先啟動。并讓它根據(jù)某個(gè)時(shí)間間隔觸發(fā)呼叫invalidate()
。invalidate()
將導(dǎo)致View.onDraw()
被調(diào)用,以便您可以重新繪制汽車的位置。當(dāng)接收到 MotionEvent.ACTION_UP 時(shí),取消Timer
mUserTouched = false'
如果有界限,那么每當(dāng)MotionEvent
發(fā)生在界限之外時(shí),您還需要進(jìn)行設(shè)置。

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
可以onTouch這樣完成onTouchEvent:
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
// Player has touched the screen
case MotionEvent.ACTION_DOWN:
System.out.println("Hallo");
break;
// Player has removed finger from screen
case MotionEvent.ACTION_UP:
break;
}
return true;
}
添加回答
舉報(bào)