본문 바로가기

Programing (프로그래밍)/Android Studio (안드로이드 스튜디오)

[ 안드로이드 스튜디오 ] 터치와 다중 터치 이벤트 처리하기

728x90
반응형

【 안드로이드 스튜디오 】 터치와 다중 터치 이벤트 처리하기




 @ ... 아직 잘... 모르겠다.... ㅠㅠ

일단 아는대로만 설명해야겠다. ㅠㅠ

아는것이 없긴하지만...


m.getPointerCount()를 통하여 터치포인트 개수를 가져온다.

각 개수를 포인트의 ID 값으로 부여하고 각각의 ID에 맞는 x,y 좌표와 Action 을 가져온다.

첫번째 Touch 를 구분하기 위해 id값이 0인경우 터치값의 기준점이 되므로 따로 빼준다.


하나이상의 터치포인터가 동시에 발생되면 그 터치들을 포인터고하며, Action_pointer_dwn 등의 액션포인터가 발생된다.





@ activity_Motion_event.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
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.admin.motionevent.Motion_event">
 
    <android.support.constraint.ConstraintLayout
        android:id="@+id/activity_motion_event"
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="8dp">
 
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            tools:text="@string/textView1_txt" />
 
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView"
            app:layout_constraintVertical_bias="0.099"
            tools:text="@string/textView2" />
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
 
cs



@ Motion_event.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
package com.example.admin.motionevent;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.support.constraint.ConstraintLayout;
import android.widget.TextView;
 
import org.w3c.dom.Text;
 
public class Motion_event extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_motion_event);
 
        ConstraintLayout myLayout = (ConstraintLayout)findViewById(R.id.activity_motion_event);
        myLayout.setOnTouchListener(
                new ConstraintLayout.OnTouchListener(){
                    public boolean onTouch(View v, MotionEvent m) {
                        handleTouch(m);
                        return true;
                    }
                }
        );
    }
 
    void handleTouch(MotionEvent m ){
        TextView textView = (TextView)findViewById(R.id.textView);
        TextView textView2 = (TextView)findViewById(R.id.textView2);
 
        int pointerCount = m.getPointerCount();
 
        for (int i = 0; i < pointerCount;i++)
        {
            int x = (int)m.getX(i);
            int y= (int)m.getY(i);
            int id = m.getPointerId(i);  // 포인터의 ID  값을 가져온다. ( 몇번째 터치인지 구분하기위한 ID )
            int action = m.getActionMasked();
            int actionIndex = m.getActionIndex();
            String actionString;
 
            switch(action)  // action 값은 각각 아래와 같다. 다운 / 업 / 이동
            {
                case MotionEvent.ACTION_DOWN:
                    actionString = "DOWN";
                    break;
                case MotionEvent.ACTION_UP:
                    actionString = "UP";
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    actionString = "PNTR DOWN";
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    actionString = "PNTR UP";
                    break;
                case MotionEvent.ACTION_MOVE:
                    actionString = "PNTR MOVE";
                    break;
                default:
                    actionString = "";
            }
 
            String touchstatus = "Action: " + actionString + " Index: " + actionIndex + " ID: " + id + "X: " + x  + "Y: " + y;
 
            if(id ==0)  textView.setText(touchstatus);   // 가장 첫번째 터치포인트이다.
            else textView2.setText(touchstatus);   // 이후에 선택한 터치 포인트이다.
        }
    }
}
 
cs




http://insurang.tistory.com


728x90
반응형