AndroidジェスチャースライドGestureDetectorとOnGestureListener(一)

4416 ワード

main.xmlは次のとおりです.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="         Button   Gesture"
        android:gravity="center"
        android:textSize="25sp"
     />
    
    <Button
        android:id="@+id/button"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:text="Button"
        android:layout_centerInParent="true"
        />

</RelativeLayout>

 
MainActivityは次のとおりです.
package cn.com.testgesture1;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.app.Activity;

/**
 * Demo  :
 * Activity        GestureDetector
 * 
 * Activity      :
 * 1   Activity onTouchEvent  
 * 2  onTouchEvent  Touch    GestureDetector   :
 *   return mGestureDetector.onTouchEvent(event);
 * 
 *          :
 * 1      setOnTouchListener
 * 2      onTouch        GestureDetector   :
 *   return mGestureDetector.onTouchEvent(event);
 *   
 *   :
 * onScroll() onFling()     :
 * 1 onScroll() happens after the user puts his finger down on
 * the screen and slides his finger across the screen without lifting it.
 * onFling() happens if the user scrolls and then lifts his finger.
 * A fling is triggered only if the motion was fast enough.
 * 2 onScroll()       MotionEvent e1, MotionEvent e2    :
 *  ACTION_DOWN(0) ACTION_MOVE(2)
 *    :onFling()       MotionEvent e1, MotionEvent e2    :
 *  ACTION_DOWN(0) ACTION_UP(1)
 */
public class MainActivity extends Activity {
	private GestureDetector mGestureDetector;
    private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init() {
		mGestureDetector = new GestureDetector(MainActivity.this,new GestureListenerImpl());
		mButton=(Button) findViewById(R.id.button);
		mButton.setOnTouchListener(new OnTouchListener() {			
			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				return mGestureDetector.onTouchEvent(event);
			}
		});
	}

	//  Activity onTouchEvent  
	// Touch    GestureDetector  
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}

	//   OnGestureListener   
	private class GestureListenerImpl implements OnGestureListener {
		//            
		@Override
		public boolean onDown(MotionEvent e) {
			System.out.println("--->      onDown   ");
			return false;
		}

		//               
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
			System.out.println("--->      onFling   ");
			System.out.println("e1.getAction()="+e1.getAction()+",e2.getAction()="+e2.getAction());
			System.out.println("velocityX="+velocityX+",velocityY="+velocityY);
			return false;
		}

		//              
		@Override
		public void onLongPress(MotionEvent e) {
			System.out.println("--->      onLongPress   ");
		}

		//               
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {
			System.out.println("--->      onScroll   ");
			System.out.println("e1.getAction()="+e1.getAction()+",e2.getAction()="+e2.getAction());
			System.out.println("distanceX="+distanceX+",distanceY="+distanceY);
			return false;
		}

		//       ,             
		@Override
		public void onShowPress(MotionEvent e) {
			System.out.println("--->      onShowPress   ");
		}

		//          
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			System.out.println("--->      onSingleTapUp   ");
			return false;
		}
	}
}