2 回答
TA貢獻1798條經驗 獲得超3個贊
設置一個全局布爾值以了解何時MotionEvent.ACTION_DOWN收到事件。
mUserTouched = true; // Set to true when MotionEvent.ACTION_DOWN mUserTouched = false; // set to false when MotionEvent.ACTION_UP is received
然后,您可能需要使用一個Timer或其他一些循環程序,每當收到 MotionEvent.ACTION_DOWN 時,該循環程序就會首先啟動。并讓它根據某個時間間隔觸發呼叫invalidate()。invalidate()將導致View.onDraw()被調用,以便您可以重新繪制汽車的位置。當接收到 MotionEvent.ACTION_UP 時,取消Timer
mUserTouched = false'如果有界限,那么每當MotionEvent發生在界限之外時,您還需要進行設置。
TA貢獻1815條經驗 獲得超6個贊
可以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;
}
添加回答
舉報
