1.GestureDetector.OnGestureListener
onDown:表示用戶的按下動(dòng)作良拼,只要用戶觸摸屏幕僵驰,不管是點(diǎn)擊、長(zhǎng)按控嗜、拖動(dòng)茧彤、滑動(dòng)都會(huì)先觸發(fā)。
onShowPress:表示用戶按下后疆栏,并沒(méi)有立刻松開(kāi)曾掂,而是停留了極短的時(shí)間后就會(huì)觸發(fā)。
onSingleTapUp:表示用戶單純的一次點(diǎn)擊事件壁顶,并沒(méi)有其他事件珠洗,就會(huì)觸發(fā)。
onScroll:表示用戶的滑動(dòng)若专,滑動(dòng)時(shí)會(huì)連續(xù)觸發(fā)许蓖。
onLongPress:表示用戶的長(zhǎng)按事件,按下后超過(guò)一定的時(shí)間會(huì)觸發(fā)调衰。
onFling:表示用戶滑動(dòng)后蛔糯,剩下的慣性會(huì)繼續(xù)滑動(dòng),就會(huì)觸發(fā)窖式。
------GestureDetector.OnGestureListener事件執(zhí)行順序------
快速點(diǎn)擊屏幕:onDown→onSingleTapUp
稍微慢速的點(diǎn)擊屏幕:onDown→onShowPress→onSingleTapUp
長(zhǎng)按屏幕:onDown→onShowPress→onLongPress
快速點(diǎn)擊屏幕后滑動(dòng)無(wú)慣性:onDown→onScroll→onScroll→onScroll...........
慢速點(diǎn)擊屏幕后滑動(dòng)無(wú)慣性:onDown→onShowPress→onScroll→onScroll→onScroll...........
快速點(diǎn)擊屏幕后滑動(dòng)有慣性:onDown→onScroll→onScroll→onScroll...........→onFling
慢速點(diǎn)擊屏幕后滑動(dòng)有慣性:onDown→onShowPress→onScroll→onScroll→onScroll...........→onFling
2.GestureDetector.OnDoubleTapListener
onSingleTapConfirmed:單擊事件蚁飒,用來(lái)判定該次點(diǎn)擊是單純的SingleTap而不是DoubleTap。
onDoubleTap:雙擊事件
onDoubleTapEvent:
------GestureDetector.OnGestureListener加上GestureDetector.OnDoubleTapListener事件執(zhí)行順序------
快速點(diǎn)擊屏幕:onDown→onSingleTapUp→onSingleTapConfirmed
稍微慢速的點(diǎn)擊屏幕:onDown→onShowPress→onSingleTapUp→onSingleTapConfirmed
雙擊屏幕:onDown→onSingleTapUp→onDoubleTap→onDoubleTapEvent→onDown→onDoubleTapEvent
3.用法
public class GestureActivity extends AppCompatActivity {
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture);
gestureDetector = new GestureDetector(this, new MyGestureListener());
gestureDetector.setOnDoubleTapListener(new MyDoubleTapListener());
ImageView imageView = findViewById(R.id.image);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
});
}
class MyGestureListener implements GestureDetector.OnGestureListener {
@Override
public boolean onDown(MotionEvent motionEvent) {
System.out.println("OnGestureListener:" + "onDown");
return true;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
System.out.println("OnGestureListener:" + "onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
System.out.println("OnGestureListener:" + "onSingleTapUp");
return true;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
System.out.println("OnGestureListener:" + "onScroll");
return true;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
System.out.println("OnGestureListener:" + "onLongPress");
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
System.out.println("OnGestureListener:" + "onFling");
return true;
}
}
class MyDoubleTapListener implements GestureDetector.OnDoubleTapListener{
@Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
System.out.println("OnDoubleTapListener:" + "onSingleTapConfirmed");
return true;
}
@Override
public boolean onDoubleTap(MotionEvent motionEvent) {
System.out.println("OnDoubleTapListener:" + "onDoubleTap");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
System.out.println("OnDoubleTapListener:" + "onDoubleTapEvent");
return true;
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
class MyContextClickListener implements GestureDetector.OnContextClickListener {
@Override
public boolean onContextClick(MotionEvent motionEvent) {
System.out.println("OnContextClickListener:" + "onContextClick");
return true;
}
}
}