본문 바로가기
매일코딩/안드로이드

[안드로이드 기초] 손가락 터치 제스쳐 이벤트

by 인생여희 2017. 8. 16.
반응형

손가락 터치 제스쳐 이벤트


#화면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
37
38
39
40
41
42
43
44
45
46
47
48
<?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>
 
    <View
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/holo_orange_dark"></View>
 
    <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="#000000"
            android:textSize="16dp" />
 
    </ScrollView>
 
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:background="@drawable/my_selector"
        android:inputType="text"
        android:text=""
        />
 
</LinearLayout>
 
cs


참고!

android:background="@drawable/my_selector"

배경화면을 drawable 안에 있는 xml로 만들었다.





#main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package me.happygate.myapplication777;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
 
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 curY = event.getY();
                float curX = event.getX();
 
 
 
                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;
            }
        });
 
 
 
 
 
        //제스쳐
        detector = new GestureDetector(thisnew 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;
            }
        });
 

//두번째 view에 제스쳐 했을 때
        View view2 = findViewById(R.id.view2);
        view2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });
    }
 
 
 
    //키가 눌렸을 때 발생하는 이벤트
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
 
        if(keyCode == KeyEvent.KEYCODE_BACK){
 
            Toast.makeText(this,"시스템 back 버튼 눌림",Toast.LENGTH_LONG).show();
            return true;
        }
 
            return  false;
    }
 
    //출력 함수
    public void println(String data){
        textView.append(data+"\n");
    }
}
 
cs



반응형

댓글