티스토리 뷰
반응형
목적
viewpager를 사용할 수 없고, 화면 전환이나 그와 유사한 이벤트가 필요하여 사용.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import android.view.View.OnTouchListener; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.GestureDetector.SimpleOnGestureListener; 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.xxxx); 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; } }; | cs |
문제점.
제스쳐를 설정하였지만, 이벤트가 scrollview에서는 먹지 않는 문제 발생.
위 문제를 해결하기 위한 특정 Layout 상위에서 Gesture를 사용할 때.
필자는 youtube player 위에서 제스쳐를 사용하기 위해 사용.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import android.view.View.OnTouchListener; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.GestureDetector.SimpleOnGestureListener; 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; private ScrollView scrView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xxxx); mContext = this; gestureScanner = new GestureDetector(new MyGestureDetector()); scrView = (ScrollView) findViewById(R.id.detail_scrView); View.OnTouchListener gestureListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub return gestureScanner.onTouchEvent(event); } }; scrView.setOnTouchListener(gestureListener); } class MyGestureDetector extends SimpleOnGestureListener { @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(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show(); } // left to right swipe else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show(); } // down to up swipe else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(getApplicationContext(), "Swipe up", Toast.LENGTH_SHORT).show(); } // up to down swipe else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(getApplicationContext(), "Swipe down", Toast.LENGTH_SHORT).show(); } return false; } } | cs |
Scrollview나 Youtube등에서 Gesture가 잘 동작 하는것을 확인 하였으나, 내가 원하는 만큼의 만족도를 따라오지는 못한다.
특히 youtube에서 기본 컨트롤러가 활성화 되어 있을 경우 swipe 동작을 인지 못한다.
동작 하더라도 인식률이 70~80정도 되는 것 같다.
향후에 코드를 재사용 할 것 같아 남긴다.
반응형
'Android > MAC에서 시작하기' 카테고리의 다른 글
[android]XML 자동완성은 되는데 "layout_"으로 시작되는 냉용은 나오지 않을 때. (0) | 2015.04.29 |
---|---|
[android] 키패드 활성화 시 컨텐츠 가려지는 문제 해결. (0) | 2015.03.26 |
[andoird] GCM 테스트용 서버 프로젝트 (0) | 2015.02.09 |
[android] SVN 소스 관리중 bin, gen 폴더 ignore 시키기. (commint 에러) (0) | 2015.01.27 |
[android][error]Unable to resolve target 'Google Inc.:Google APIs:18' (0) | 2015.01.15 |
댓글
반응형