2013年12月2日星期一

手勢檢測 GestureDetector.SimpleOnGestureListener

GestureDetector.SimpleOnGestureListener 類為檢測手勢(Gesture)提供一種簡單的方式. 它實現了 GestureDetector.OnGestureListener 和 GestureDetector.OnDoubleTapListener 的所有方法. 你只需要擴展 GestureDetector.SimpleOnGestureListener 子類,並重寫所需的方法便可以。

返回值: 如果該事件被消耗掉 return true,否則 return false.

示例代碼:

package com.example.androidsimpleongesturelistener;

import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {
 
 private GestureDetectorCompat myGestureDetector;
 String info;
 TextView infoView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.activity_main); 
  infoView = new TextView(this);
  setContentView(infoView);

  info = "";
  infoView.setText(info);
  
  myGestureDetector = 
    new GestureDetectorCompat(this, new MyGestureListener());
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  this.myGestureDetector.onTouchEvent(event);
  return super.onTouchEvent(event);
 }

 class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

  @Override
  public boolean onDoubleTap(MotionEvent e) {
   info += "onDoubleTap - " + 
     e.getX() + " : " + e.getY() + 
     "\n";
   infoView.setText(info);
   return super.onDoubleTap(e);
  }

  @Override
  public boolean onDoubleTapEvent(MotionEvent e) {
   info += "onDoubleTapEvent - " + 
     e.getX() + " : " + e.getY() + 
     "\n";
   infoView.setText(info);
   return super.onDoubleTapEvent(e);
  }

  @Override
  public boolean onDown(MotionEvent e) {
   info = "onDown - " + 
     e.getX() + " : " + e.getY() + 
     "\n";
   infoView.setText(info);
   return super.onDown(e);
  }

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
   info += "onFling - " + "\n" +
     " e1 - " + e1.getX() + " : " + e1.getY() + "\n" +
     " e2 - " + e2.getX() + " : " + e2.getY() + "\n" +
     " velocityX = " + velocityX + "\n" +
     " velocityY = " + velocityY + "\n";
   infoView.setText(info);
   return super.onFling(e1, e2, velocityX, velocityY);
  }

  @Override
  public void onLongPress(MotionEvent e) {
   info += "onLongPress - " + 
     e.getX() + " : " + e.getY() + 
     "\n";
   infoView.setText(info);
   super.onLongPress(e);
  }

  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2,
    float distanceX, float distanceY) {
   /*
   info += "onScroll - " + "\n" +
     " e1 - " + e1.getX() + " : " + e1.getY() + "\n" +
     " e2 - " + e2.getX() + " : " + e2.getY() + "\n" +
     " distanceX = " + distanceX + "\n" +
     " distanceY = " + distanceY + "\n";
   infoView.setText(info);
   */
   info += "onScroll\n";
   infoView.setText(info);
   return super.onScroll(e1, e2, distanceX, distanceY);
  }

  @Override
  public void onShowPress(MotionEvent e) {
   info += "onShowPress - " + 
     e.getX() + " : " + e.getY() + 
     "\n";
   infoView.setText(info);
   super.onShowPress(e);
  }

  @Override
  public boolean onSingleTapConfirmed(MotionEvent e) {
   info += "onSingleTapConfirmed - " + 
     e.getX() + " : " + e.getY() + 
     "\n";
   infoView.setText(info);
   return super.onSingleTapConfirmed(e);
  }

  @Override
  public boolean onSingleTapUp(MotionEvent e) {
   info += "onSingleTapUp - " + 
     e.getX() + " : " + e.getY() + 
     "\n";
   infoView.setText(info);
   return super.onSingleTapUp(e);
  }
 }
}


手勢檢測 GestureDetector.SimpleOnGestureListener
手勢檢測 GestureDetector.SimpleOnGestureListener


相關帖子:
- 實現 GestureDetector.OnGestureListener 檢測手勢

沒有留言:

發佈留言