2015年8月15日土曜日

Android開発 フリックイベントを取得する


Androidでフリックイベントを取得してみます。

http://developer.android.com/intl/ja/reference/android/view/GestureDetector.html
GestureDetector クラスを使うことで詳細なタッチイベントを処理できるようになります。
そのなかの onFling メソッド(タップしてから移動して指が離れるまでの情報を扱う)を利用します。
public abstract boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
パラメータは以下のようになります。
e1 The first down motion event that started the fling.
e2 The move motion event that triggered the current onFling.
velocityX The velocity of this fling measured in pixels per second along the x axis.
velocityY The velocity of this fling measured in pixels per second along the y axis.

実装するにあたって、下記の資料が参考になりました。
スワイプのイベントを取得する - ほげほげ(仮)
http://starzero.hatenablog.com/entry/20110414/1302759479
【Android】onFlingでフリック時の移動距離を取得する|俺メモ Web時々アプリ | OREMEMO
http://www.ore-memo.com/564.html
タッチパネルのダブルタップや長押しを検出する « Tech Booster
http://techbooster.jpn.org/andriod/device/3936/#more-3936

以下、サンプルコードです。