android處理單擊雙擊和滑動事件

yangxi_001發表於2015-01-12
android有處理滑鼠動作的GestureDetector,使用起來也挺方便,幾個常用事件你都可以找到介面接入處理:

單擊、雙擊(比如雙擊放大)、滑動(比如翻頁)
下面是簡化的例子,參考並整合所得,來源參考頁尾參考內容:

package com.aslibra.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class touchGesture extends Activity implements OnGestureListener {       
  private LinearLayout main;       
  private TextView viewA;
  private GestureDetector gestureScanner;

  @Override  
  public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   

    gestureScanner = new GestureDetector(this);   
    gestureScanner.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener(){  
      public boolean onDoubleTap(MotionEvent e) {  
        viewA.setText("-" + "onDoubleTap" + "-");  
        //雙擊時產生一次
        Log.v("test", "onDoubleTap");
        return false;  
      }
      public boolean onDoubleTapEvent(MotionEvent e) {  
        //雙擊時產生兩次
        Log.v("test", "onDoubleTapEvent");
        return false;
      }  
      public boolean onSingleTapConfirmed(MotionEvent e) {  
        viewA.setText("-" + "onSingleTapConfirmed" + "-");  
        //短快的點選算一次單擊
        Log.v("test", "onSingleTapConfirmed");
        return false;  
      }  
    }); 


    main = new LinearLayout(this);   
    main.setBackgroundColor(Color.GRAY);
    main.setLayoutParams(new LinearLayout.LayoutParams(320,480)); 
    main.setOrientation(LinearLayout.VERTICAL);

    viewA = new TextView(this);
    viewA.setBackgroundColor(Color.YELLOW);
    viewA.setTextColor(Color.BLACK);   
    viewA.setTextSize(16);
    viewA.setLayoutParams(new LinearLayout.LayoutParams(320,50));   
    main.addView(viewA);

    setContentView(main);   
  }

  @Override  
  public boolean onTouchEvent(MotionEvent me) {   
    return gestureScanner.onTouchEvent(me);   
  }   

  @Override  
  public boolean onDown(MotionEvent e) {   
    //viewA.setText("-" + "DOWN" + "-");   
    return true;   
  }   

  @Override  
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {   
    //viewA.setText("-" + "FLING" + "- "+velocityX + "- "+velocityY);   
    Log.v("test", "onFling "+e1.getX()+" "+e2.getX());
    return true;   
  }   

  @Override  
  public void onLongPress(MotionEvent e) {   
    //viewA.setText("-" + "LONG PRESS" + "-");   
  }   

  @Override  
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {   
    //viewA.setText("-" + "SCROLL" + "- "+distanceX + "- "+distanceY);   
    Log.v("test", "onScroll "+e1.getX()+" "+e2.getX());
    return true;   
  }   

  @Override  
  public void onShowPress(MotionEvent e) {   
    //viewA.setText("-" + "SHOW PRESS" + "-");   
  }

  @Override  
  public boolean onSingleTapUp(MotionEvent e) {   
    Log.v("test", "onSingleTapUp");
    //viewA.setText("-" + "SINGLE TAP UP" + "-"+ i++);   
    return true;   
  }   
  
}  

相關文章