OnGestureListener 可以處理一些簡單的觸摸手勢,比如滑動,長按等,
OnGestureListener不能直接設置偵聽的,可以設置OnTouchListener,讓其指向OnGestureListener
public class GestureView extends View implements OnTouchListener, OnGestureListener
{
GestureDetector mGestureDetector;
public GestureView(Context context)
{
super(context);
this.setOnTouchListener(this);
mGestureDetector = new GestureDetector(this);
}
@Override
public boolean onDown(MotionEvent e)
{
Log.i("@fred", "觸摸手勢:onDown");
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
if (e2.getX() - e1.getX() > 50)
{
Log.i("@fred", "觸摸手勢:onFling");
}
return true;
}
@Override
public void onLongPress(MotionEvent e)
{
Log.i("@fred", "觸摸手勢:onLongPress");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
Log.i("@fred", "觸摸手勢:onScroll");
return true;
}
@Override
public void onShowPress(MotionEvent e)
{
Log.i("@fred", "觸摸手勢:onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
Log.i("@fred", "觸摸手勢:onSingleTapUp");
return true;
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
// 將MotionEvent事件傳到GestureDetector
return mGestureDetector.onTouchEvent(event);
}
}
比如點擊一下屏幕:
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11