天天看點

GestureDetector-手勢

手勢(GestureDetector類) //手勢監聽GestureDetector.OnGestureListener

  • boolean onDown(MotionEvent e),按下事件
  • boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY),在螢幕上拖動,e1與e2為拖動前後的位置,veloctiyX/Y表示拖動在橫向與縱向的速度
  • abstract void onLongPress(MotionEvent e),長按螢幕觸發該事件
//使用手勢監聽類處理手勢
//建立GestureDetector類
GestureDetector detector=new GestureDetector(this,this);
//将Acitivity上的觸碰事件交給GestureDetector處理
public boolean onTouchEvent(MotionEvent me){
    //将事件交給GestureDetector處理,完成此步操作,重寫onDown(),onFling()等方法即可由GestureDetector處理
    return detector.onTouchEvent(me);
}
           

//自定義手勢 //使用GestureLibraies方法加載手勢庫

  • static GestureLibrary fromFile(String path),從path路徑檔案加載手勢庫
  • static GestureLibrary fromFile(File path),從path檔案加載手勢庫
  • static GestureLibrary fromPrivateFile(Context context,String name),從指定應用程式資料路徑檔案name中加載手勢庫
  • static GestureLibrary fromRawResource(Context context,int resourceid),從resourceid代表的資源中加載手勢庫

//添加手勢API

  • void addGesture(String entryName,Gesture gesture),添加一個名為entryName的手勢
  • ArrayList<Gesture> getGestures(String entryName),擷取名為entryName的所有手勢
  • ArrayList<Prediction> recognize(Gesture gesture),從目前手勢庫識别與gesture比對的全部手勢
  • void removeGesture(String entryName,Gesture gesture),删除名為entryName的手勢
  • boolean save(),添加手勢或删除手勢後儲存手勢庫