1.Touch Event & Gesture Event 결과 화면
2. MainActivity.java
MotionEvent 객체에는 터치된 곳의 좌표와 함께 터치된 상태가 전달된다.
손가락이 눌렸는지, 눌린 상태에서 움직였는지, 아니면 손가락이 떼어졌는지를 구분할 수 있는 정보를 받을 수 있는데 getAction 메소드를 이용해 확인할 수 있다.
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 | package com.tistory.qlyh8.pracitice; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; View view1, view2; GestureDetector detector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textview); view1 = findViewById(R.id.view1); view2 = findViewById(R.id.view2); view1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) { println("손가락 눌림."); } else if (action == MotionEvent.ACTION_MOVE) { println("손가락 움직임."); } else if (action == MotionEvent.ACTION_UP) { println("손가락 뗌."); } return true; } }); view2.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { detector.onTouchEvent(event); return true; } }); detector = new GestureDetector(this, new GestureDetector.OnGestureListener() { @Override public boolean onDown(MotionEvent e) { println("onDown() 호출됨."); return true; } @Override public void onShowPress(MotionEvent e) { println("onShowPress() 호출됨."); } @Override public boolean onSingleTapUp(MotionEvent e) { println("onSingleTapUp() 호출됨."); return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { println("onScroll() 호출됨." + distanceX + ", " + distanceY); return true; } @Override public void onLongPress(MotionEvent e) { println("onLongPress() 호출됨."); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { println("onFling() 호출됨." + velocityX + ", " + velocityY); return true; } }); } public void println(String data){ textView.append(data + "\n"); } } | cs |
4. activity_main.xml
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 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.tistory.qlyh8.pracitice.MainActivity" android:orientation="vertical"> <View android:id="@+id/view1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#e76a6a"/> <View android:id="@+id/view2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#ade76a"/> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout> | cs |
출처: https://www.edwith.org/boostcourse-android/lecture/17051/
'Android' 카테고리의 다른 글
Toast (토스트) (0) | 2018.04.28 |
---|---|
Event (이벤트) - 키 이벤트 (0) | 2018.04.28 |
Event (이벤트) (0) | 2018.04.28 |
ScrollView (스크롤뷰) (0) | 2018.04.28 |
Table Layout (테이블 레이아웃) (0) | 2018.04.28 |