original source : https://youtu.be/KPofPZLTuMc

keyevent는 실제 물리적인 버튼에 의해 발생한 이벤트를 말한다. 예를 들면 음량버튼

motionevent는 터치이벤트를 말한다.

image
image

==========================================================

.

.

image
image

==========================================================

.

.

참고자료) https://stackoverflow.com/a/42040411/3151712

참고자료) https://medium.com/@arj.sna/multi-touch-in-android-c87031d106b3

Multi-Touch in android was available since Android 2.0. When more than one finger touches the screen multi-touch gesture happens and android provide various apis to handle these gestures. Lets start with the basic event handling for single finger on the screen

public boolean onTouchEvent(MotionEvent ev){ 
 final int action = ev.getAction();
 switch(action) {
    case MotionEvent.ACTION_DOWN: {
       Log.i("Event handled ", "Action finger down"); break;
    }
    case MotionEvent.ACTION_UP: {
      Log.i("Event handled ", "Action finger up");
      break;
    }
 return true;
}

The above is the one of many methods of handling touches which logs touch down and up of single finger. This work fine as long as we don’t need to handle multiple fingers. But in many cases handling single finger events in not sufficient. For handling multi-touch we need to handle the below two additional events.

ACTION_POINTER_DOWN – MotionEvent.ACTION_POINTER_DOWN is the event triggered whenever the secondary pointer touches the screen. If there is already a pointer(finger) on the screen and one more pointer goes in contact with the screen, ACTION_POINTER_DOWN event is triggered. For all the next coming pointers the ACTION_POINTER_DOWN is triggered. MotionEvent.ACTION_DOWN will be triggered only for the first pointer coming in contact with the screen.

ACTION_POINTER_UP – MotionEvent.ACTION_POINTER_UP is the event triggered when a pointer goes up and there in yet another pointer still in contact with the screen. The same event is triggered for all pointer going up till the last pointer and MotionEvent.ACTION_UP will be triggered for the last pointer leaving the screen.

The next two things we should know to keep track of each pointer is are Pointer Index and Pointer ID. Lets see what they are and how they help us in handling multi-touch.

Index – In a MotionEvent object, the pointer details are stored in array. So, the Index of a pointer is its position in that array. From this we can infer that pointer index in not persistent across the event. To put that with an example, when a first pointer touches the screen, it will be the first object in array, with pointer index 0. When second pointer comes, it will be stored in index 1. Now when the pointer with index 0 goes up, the position of pointer in array is rearranged, which puts the second pointer which is currently stays on screen in index 0.

ID – While the pointer index is non persistent, to identify the pointers across events of an entire gesture, Pointer IDcomes in handy. Pointer ID is unique ID mapping to the pointer which stays persistent always. So in the example discussed for Index, even when the position of pointer in array changes, its pointer id remains same. That makes the difference between Index and ID.

image
image

==========================================================

.

.

image
image

==========================================================

.

.

MotionEvent를 통해 id를 얻을 수도 있고 index를 얻을수도 있다.

MotionEvent.getPointerId()

MotionEvent.findPointerIndex()

각각의 터치는 id를 가지며 id를 통해 index를 얻고 index를 통해 x, y 좌표를 얻을수 있다.

image
image
image

==========================================================

.

.

image

==========================================================

.

.

image
image

==========================================================

.

.

image
image

==========================================================

.

.

image

==========================================================

.

.

하나의 화면에서 스크롤기능과 클릭이 합쳐진 경우

예를 들어 listview를 스크롤하는 경우. view group에서 먼저 처리할 이벤트를 처리해야 하는 경우가 있다. 

image
image

==========================================================

.

.

image
image
image

==========================================================

.

.

image

==========================================================

.

.

image
image

==========================================================

.

.

image

Comments are closed.

Post Navigation