前言
這次主要講解一些Android簡單手勢的識別,主要用到的是GestureDetector,SimpleOnGestureListener,OnGestureListener等類
實現原理
我們想要實現手勢的識別,當然要監聽觸摸屏事件,主要流程是:重寫View的onTouchEvent事件,將攔截到的MotionEvent事件交給GestureDetector類的OnGestureListener和OnDoubleTapListener處理。我們可以分別處理OnGestureListener和OnDoubleTapListener中的事件,因爲我們不會用到所有的事件,所以我們也可以直接使用GestureDetector.SimpleOnGestureListener,這是一個實現了OnGestureListener接口和OnDoubleTapListener接口的類。
攔截事件
如果我們要攔截整個Activity的觸摸屏事件,重寫onTouchEvent(MotionEvent event)即可,當然我們可以爲單獨的 View設置監聽事件,view.setOnTouchListener,爲View添加監聽事件,然後將MotionEvent事件分發給GestureDetector處理mGestureDetector.onTouchEvent(event);
處理事件
SimpleOnGestureListener中的事件:
boolean onDoubleTap(MotionEvent e)
// Notified when a double-tap occurs.
boolean onDoubleTapEvent(MotionEvent e)
//Notified when an event within a double-tap gestureoccurs, including the down, move, and up events.
boolean onDown(MotionEvent e)
// Notified when a tap occurs with the down MotionEvent that triggered it.
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
// Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent.
void onLongPress(MotionEvent e)
//Notified when a long press occurs with the initial on down MotionEvent that trigged it.
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
//Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent.
void onShowPress(MotionEvent e)
//The user has performed a down MotionEvent and not performed a move or up yet.
boolean onSingleTapConfirmed(MotionEvent e)
// Notified when a single-tap occurs.
boolean onSingleTapUp(MotionEvent e)
// Notified when a tap occurs with the up MotionEvent that triggered it.
英語太渣,不給你們翻譯了 - -。
示例代碼
GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
mGestureDetector = new GestureDetector(this, new MyGestureListener());
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return mGestureDetector.onTouchEvent(event);
}
class MyGestureListener extends SimpleOnGestureListener
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_SHORT).show();
return super.onDoubleTap(e);
}
@Override
public boolean onDoubleTapEvent(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onDoubleTapEvent", Toast.LENGTH_SHORT).show();
return super.onDoubleTapEvent(e);
}
@Override
public boolean onDown(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_SHORT).show();
if (e1.getX() - e2.getX() > 50)
{
Toast.makeText(MainActivity.this, "從右向左劃", Toast.LENGTH_SHORT)
.show();
} else if (e2.getX() - e1.getX() > 50)
{
Toast.makeText(MainActivity.this, "從左向右劃", Toast.LENGTH_SHORT)
.show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onLongPress(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_SHORT).show();
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_SHORT).show();
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public void onShowPress(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
super.onShowPress(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onSingleTapConfirmed", Toast.LENGTH_SHORT).show();
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return super.onSingleTapUp(e);
}
}
這是簡單手勢的識別,下一篇講解一下自定義的手勢識別。
下篇:自定義手勢識別