android gesture example, swipe(flicking)

(간단한 안드로이드 제스춰 예제, 플리킹)


MainActivity.java


package com.example.samplegesture;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends Activity  {  

    Context mContext;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureScanner;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = this;
		gestureScanner = new GestureDetector(this, mGestureListener );

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		return gestureScanner.onTouchEvent(event);	// event 전달
	}
	
	OnGestureListener mGestureListener = new OnGestureListener() {
		
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			Toast.makeText(mContext, "onSingleTapUp", Toast.LENGTH_SHORT).show();
			return false;
		}
		
		@Override
		public void onShowPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
	        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
	        	return false;
		      // right to left swipe
		      if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
		          Toast.makeText(mContext, "Left Swipe", Toast.LENGTH_SHORT).show();
		      }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
		          Toast.makeText(mContext, "Right Swipe", Toast.LENGTH_SHORT).show();
		      }
			return false;
		}
		
		@Override
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			return false;
		}
	};
	
}

참고 : http://stackoverflow.com/questions/937313/android-basic-gesture-detection


+ Recent posts