- onSingleTapUp : 按一下(按下並放開)。
- onLongPress : 長按(按下不放開)。
- onScroll : 捲動、捲動(按下移動沒有放開)。
- onFling : 滑動(按下移動並放開)。
- onShowPress : 按住(按下沒放開)。
- onDown : 按下。
- onDoubleTap : 按兩下(算一次事件)。
- onDoubleTapEvent : 按兩下(算兩次事件)。
- onSingleTapConfirmed : 確定按一下。
下面我們就寫一個簡單的程式碼來試試看各種動作的觸發狀況:
package com.example.administrator.gesturedetector_example; import android.support.v4.view.GestureDetectorCompat; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.widget.Toast; public class MainActivity extends ActionBarActivity { GestureDetector gestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gestureDetector = new GestureDetector (this,new GestureDetector.SimpleOnGestureListener(){ @Override public boolean onSingleTapUp(MotionEvent e) { Toast.makeText(getApplicationContext(),"手勢偵測: onSingleTapUp",Toast.LENGTH_SHORT).show(); return super.onSingleTapUp(e); } @Override public void onLongPress(MotionEvent e) { Toast.makeText(getApplicationContext(),"手勢偵測: onLongPress",Toast.LENGTH_SHORT).show(); super.onLongPress(e); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Toast.makeText(getApplicationContext(),"手勢偵測: onScroll",Toast.LENGTH_SHORT).show(); return super.onScroll(e1, e2, distanceX, distanceY); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Toast.makeText(getApplicationContext(),"手勢偵測: onFling",Toast.LENGTH_SHORT).show(); return super.onFling(e1, e2, velocityX, velocityY); } @Override public void onShowPress(MotionEvent e) { Toast.makeText(getApplicationContext(),"手勢偵測: onShowPress",Toast.LENGTH_SHORT).show(); super.onShowPress(e); } @Override public boolean onDown(MotionEvent e) { Toast.makeText(getApplicationContext(),"手勢偵測: onDown",Toast.LENGTH_SHORT).show(); return super.onDown(e); } @Override public boolean onDoubleTap(MotionEvent e) { Toast.makeText(getApplicationContext(),"手勢偵測: onDoubleTap",Toast.LENGTH_SHORT).show(); return super.onDoubleTap(e); } @Override public boolean onDoubleTapEvent(MotionEvent e) { Toast.makeText(getApplicationContext(),"手勢偵測: onDoubleTapEvent",Toast.LENGTH_SHORT).show(); return super.onDoubleTapEvent(e); } @Override public boolean onSingleTapConfirmed(MotionEvent e) { Toast.makeText(getApplicationContext(),"手勢偵測: onSingleTapConfirmed",Toast.LENGTH_SHORT).show(); return super.onSingleTapConfirmed(e); } }); } @Override public boolean onTouchEvent(MotionEvent event) { return gestureDetector.onTouchEvent(event); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
沒有留言 :
張貼留言