반응형

It describes how to use the touch API in Android applications.


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
public class MainActivity extends AppCompatActivity {
 
    int[] id = new int[3];
    int[] x = new int[3];
    int[] y = new int[3];
    String result;
 
    TextView textView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        textView = findViewById(R.id.textView);
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
 
        // 3개의 포인터(터치)까지 허용
        int pointer_count = event.getPointerCount();
        if (pointer_count > 3)
            pointer_count = 3;
 
        // ACTION_POINTER_DOWN 과 ACTION_POINTER_UP 이벤트는 액션 값에 추가적인 정보가 인코딩 되어 있다.
        // 액션 값과 MotionEvent.ACTION_POINTER_INDEX_MASK를 & 연산을 하게 되면 눌리거나 떼어진 포인터의 인덱스 값을 알 수 있다.
        // public static final int ACTION_POINTER_INDEX_MASK
        // Bits in the action code that represent a pointer index, used with ACTION_POINTER_DOWN and ACTION_POINTER_UP.
        switch (event.getAction() & MotionEvent.ACTION_MASK)
        {
            case MotionEvent.ACTION_DOWN:
                result = "Single Touch Down:";
                id[0= event.getPointerId(0);
                x[0= (int)event.getX(0);
                y[0= (int)event.getY(0);
                result += "\n(" + x[0+ ", " + y[0+ ")";
                break;
 
            case MotionEvent.ACTION_POINTER_DOWN:
                result = "Multi Touch Down:";
                for (int i = 0; i < pointer_count; i++)
                {
                    id[i] = event.getPointerId(i);
                    x[i] = (int)event.getX(i);
                    y[i] = (int)event.getY(i);
                    result += "\n(" + id[i] + ": " + x[i] + ", " + y[i] + ")";
                }
 
                // Move는 싱글, 멀티 모두 ACTION_MOVE 하나로 처리
            case MotionEvent.ACTION_MOVE:
                result = "Touch Move:";
                for (int i = 0; i < pointer_count; i++)
                {
                    id[i] = event.getPointerId(i);
                    x[i] = (int)event.getX(i);
                    y[i] = (int)event.getY(i);
                    result += "\n(" + id[i] + ": " + x[i] + ", " + y[i] + ")";
                }
                break;
 
            case MotionEvent.ACTION_UP:
                result = "Single Touch Up:";
                id[0= event.getPointerId(0);
                x[0= (int)event.getX(0);
                y[0= (int)event.getY(0);
                result += "\n(" + x[0+ ", " + y[0+ ")";
                break;
 
            case MotionEvent.ACTION_POINTER_UP:
                result = "Multi Touch Up:";
                for (int i = 0; i < pointer_count; i++)
                {
                    id[i] = event.getPointerId(i);
                    x[i] = (int) event.getX(i);
                    y[i] = (int) event.getY(i);
                    result += "\n(" + id[i] + ": " + x[i] + ", " + y[i] + ")";
                }
                break;
 
            default:
                break;
        }
 
        textView.setText(result);
 
        return super.onTouchEvent(event);
    }
}



Run the app and touch the panel.


반응형
Posted by J-sean
: