android觸屏手勢識別全解析

android中使用觸屏觸控技術和手勢識別技術,有許多細節問題,這裏整理一下,供大家參考。
package com.ethan.touch;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;

/**
* 測試觸摸和手勢
*
* @author ethan 實現了觸控監聽和手勢監聽
*/

public class TouchGestureActivity extends Activity implements OnTouchListener,
    OnGestureListener {

  private static final String tag = "TouchGestureActivity";

  public GestureDetector mGestureDetector;
  TextView textView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // 初始化手勢識別對象,有時候很容易忘記,就會報空指針異常
    mGestureDetector = new GestureDetector(this);
    textView = (TextView) this.findViewById(R.id.textView);
    textView.setOnTouchListener(this);
    textView.setLongClickable(true);// 不設置這行,onFling可能監聽不到

  }

  /*
    * 觸摸事件監聽(這裏既然用了手勢,就需要將觸摸事件的event傳遞給手勢的監聽)
    */

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    // OnGestureListener will analyzes the given motion event
    return mGestureDetector.onTouchEvent(event);
  }

  /*
    * 用戶輕觸觸摸屏,由1個MotionEvent ACTION_DOWN觸發
    */

  @Override
  public boolean onDown(MotionEvent e) {
    Log.i(tag, "onDown!");
    return false;
  }

  /*
    * 用戶按下觸摸屏、快速移動後鬆開,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE, 1個ACTION_UP觸發
    */

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
      float velocityY) {

    Log.i(tag, "onFling!");
    // 參數解釋:
    // e1:第1個ACTION_DOWN MotionEvent
    // e2:最後一個ACTION_MOVE MotionEvent
    // velocityX:X軸上的移動速度,像素/秒
    // velocityY:Y軸上的移動速度,像素/秒

    // 觸發條件 :
    // X軸的座標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒

    int FLING_MIN_DISTANCE = 100;
    int FLING_MIN_VELOCITY = 200;

    if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
        && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
      // Fling left
      Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();
    } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
        && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
      // Fling right
      Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();
    }

    return false;
  }

  /*
    * 用戶長按觸摸屏,由多個MotionEvent ACTION_DOWN觸發
    */

  @Override
  public void onLongPress(MotionEvent e) {
    Log.i(tag, "onLongPress!");
  }

  /*
    * 用戶按下觸摸屏,並拖動,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE觸發
    */

  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
      float distanceY) {
    Log.i(tag, "onScroll!");
    //參數和onFling裏面差不多
    return false;
  }

  /*
    * 用戶輕觸觸摸屏,尚未鬆開或拖動,由一個1個MotionEvent ACTION_DOWN觸發
    * 注意和onDown()的區別,強調的是沒有鬆開或者拖動的狀態
    */

  @Override
  public void onShowPress(MotionEvent e) {
    Log.i(tag, "onShowPress!");
  }

  /*
    * 用戶(輕觸觸摸屏後)鬆開,由一個1個MotionEvent ACTION_UP觸發
    */

  @Override
  public boolean onSingleTapUp(MotionEvent e) {
    Log.i(tag, "onSingleTapUp!");
    return false;
  }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章