상세 컨텐츠

본문 제목

TouchEvent 동작 원리 in Android

안드로이드/basic

by 개발혱 2022. 7. 16. 18:04

본문

매번 그림을 그려보았으나. '음 외워지지가 않는 이유는 뭘까?'

 

이 그림 대로 코드를 구현하니 동작 원리를 보지 않고도 이해하게됐다.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        // ViewGroupA   
        val customLayout = CustomLayout(this)
        addContentView(customLayout, FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT))

        // ViewGroupB
        val customView = CustomView(this)
        customView.text = "Hello, TouchEvent()"
        customLayout.addView(customView)
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        Log.d("TouchEvent", "called dispatchTouchEvent() in MainActivity(Activity)")
        return super.dispatchTouchEvent(ev)
    }
    
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.d("TouchEvent", "called onTouchEvent() in MainActivity(Activity)")
        return super.onTouchEvent(event)
    }
}

 

import android.content.Context
import android.util.Log
import android.view.MotionEvent
import android.widget.FrameLayout

class CustomLayout(context: Context) : FrameLayout(context) {

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        Log.d("TouchEvent", "called dispatchTouchEvent() in CustomLayout(ViewGroupA)")
        return super.dispatchTouchEvent(ev)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        Log.d("TouchEvent", "called onInterceptTouchEvent() in CustomLayout(ViewGroupA)")
        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.d("TouchEvent", "called onTouchEvent() in CustomLayout(ViewGroupA)")
        return super.onTouchEvent(event)
    }
}
import android.content.Context
import android.util.Log
import android.view.MotionEvent
import androidx.appcompat.widget.AppCompatTextView

class CustomView(context: Context) : AppCompatTextView(context) {

    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
        Log.d("TouchEvent", "called dispatchTouchEvent() in CustomView(ViewGroupB)")
        return super.dispatchTouchEvent(event)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.d("TouchEvent", "called onTouchEvent() in CustomView(ViewGroupB)")
        return super.onTouchEvent(event)
    }
}
D/TouchEvent: called dispatchTouchEvent() in MainActivity(Activity)
D/TouchEvent: called dispatchTouchEvent() in CustomLayout(ViewGroupA)
D/TouchEvent: called onInterceptTouchEvent() in CustomLayout(ViewGroupA)
D/TouchEvent: called dispatchTouchEvent() in CustomView(ViewGroupB)
D/TouchEvent: called onTouchEvent() in CustomView(ViewGroupB)
D/TouchEvent: called onTouchEvent() in CustomLayout(ViewGroupA)
D/TouchEvent: called onTouchEvent() in MainActivity(Activity)

 

 

참고 자료 :

- ViewGroup에서 터치 이벤트 관리

- How touch events are delivered in Android

[Android] 안드로이드의 Touch Event 는 어떻게 전달 될까? (with. Touch Intercept)

'안드로이드 > basic' 카테고리의 다른 글

Kotlin, sequenceOf  (0) 2022.10.01
GC  (0) 2022.06.15
JVM 메모리 구조  (0) 2022.06.15
[Kotlin, Android] let, with, run, apply, also  (0) 2021.05.09
동영상 보며 코루틴동영상 보며 코루틴 요약하기  (0) 2021.03.28

관련글 더보기