본문 바로가기

뷰 이벤트. 터치 이벤트 알아보기. setOnTouchListener -onTouch / GestureDetector


 이벤트  / 뷰 이벤트 

- 터치 이벤트 (View.OnTouchListener)


여기서는 

뷰 이벤트 - 터치 이벤트


화면을 손가락으로 눌렀을 때, 이동할 때, 뗄 때의 파라미터 값을 어떻게 얻는지 알아보자.




- 터치 했을 때 (터치/이동/뗄때)


- 좌표 / 이동거리 / 속도 얻기 -   GestureDetector 



---

<XML>

- 기본 레이아웃은 리니어 레이아웃으로 view 3개로 나눈다.


- 위 :    터치 할 때, 이동할 때, 뗄 때 좌표 (x값, y값)

- 중간 : 마찬가지 이지만, GestureDetector 로 이동 시, 거리 /  속도

- 아래 : 위 두가지의 event log를 textview로 출력


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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_bright" /> // 배경색 : 하늘
 
    <View
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="?android:attr/colorPressedHighlight" /> // 배경색 : 주황
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
 
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@android:color/black"
            android:textSize="16dp" />
 
 
    </ScrollView>
 
</LinearLayout>
 
cs


<MainActivity.java>


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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package myevent;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    TextView textView;
    GestureDetector detector;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        textView =(TextView)findViewById(R.id.textView);
        View view =findViewById(R.id.view);
 
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
 
                float curX = event.getX();
                float curY = event.getY();
 
                if (action == MotionEvent.ACTION_DOWN){
                    println("손가락 눌렸음 : "+curX +","+curY);
                }else if(action == MotionEvent.ACTION_MOVE){
                    println("손가락 움직임 : "+curX+","+curY);
                }else if(action == MotionEvent.ACTION_UP){
                    println("손가락 떼졌음 : "+curX+","+curY);
                }
                return true;
            }
        });
 
 
 
        // GestureDetector : 속도를 자동으로 계한해서 파라미터로 넘겨줌!
        detector = new GestureDetector(thisnew GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                println("onDown() 호출됨.");
                return false;
            }
 
            @Override
            public void onShowPress(MotionEvent e) {
                println("onShowPress() 호출됨.");
 
            }
 
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                println("onSingleTapUp() 호출됨.");
                return false;
            }
 
            @Override
            // 이동 거리
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                println("onScroll() 호출됨."+distanceX+", "+distanceY);
                return false;
            }
 
            @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 false;
            }
        });
 
        View view2 = findViewById(R.id.view2);
 
        view2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });
 
 
    }
    // textView에 data를 한 줄씩 넘겨주는 println 메소드 정의
    public void println(String data){
        textView.append(data+"\n");
 
    }
 
}
 
cs



---



화변을 터치했을 때, 다음 값을 얻을 수 있다.


1. 이벤트 - 터치 / 이동 / 뗄 때

2. 파라미터 - XY값, 이동거리, 속도