android gesture example, swipe(flicking)
(간단한 안드로이드 제스춰 예제, 플리킹)
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
'안드로이드 개발 팁' 카테고리의 다른 글
안드로이드 카메라, 동영상 기본 저장 경로 구하기 (0) | 2013.08.07 |
---|---|
android event listener 사용법 (버튼 눌렀을때 처리) (0) | 2013.07.04 |
android custom toast 만들기 (0) | 2013.06.28 |
프로그래스 다이알로그 ani (0) | 2013.06.12 |
android interface를 이용한 상태 변동시 체크 방법 (0) | 2013.06.10 |