본문으로 바로가기

[JETPACK COMPOSE] Image 버튼에 Press/Release 이벤트 넣기

category Android_app 2023. 7. 26. 15:33

: 기본적으로  Surface() 안에  Image 버튼을 다음처럼  넣어 줍니다.

Surface(modifier = Modifier
    .fillMaxSize()
) {
    Image(painter = painterResource(R.drawable.center),
        contentDescription = null
    )
}

 

▶ Modifier  안의 메서드 중 .pointerInteropFilter 를 다음처럼 넣어 줍니다. 

   >>  MotionEvent.ACTION_DOWN , ACTION_UP를 추가 하고 Log.d() 를 넣었는데 적당한 액션을 넣어줍니다. 

Surface(modifier = Modifier
    .fillMaxSize()
) {
    Image(painter = painterResource(R.drawable.center),
        contentDescription = null,
        modifier = Modifier
            .pointerInteropFilter {
                when (it.action) {
                    MotionEvent.ACTION_DOWN -> {
                        Log.d(TAG, "Pressed")
                    }
                    MotionEvent.ACTION_UP -> {
                        Log.d(TAG, "Released")
                    }
                    else -> false
                }
                true

            }
    )
}

 

 

  이미지 클릭시 Logcat

D/test_app::MainActivity: Pressed
D/test_app::MainActivity: Released
D/test_app::MainActivity: Pressed
D/test_app::MainActivity: Released
D/test_app::MainActivity: Pressed
D/test_app::MainActivity: Released

 

 

 

 

참고용 자료입니다.

오늘도 수고하세요.

반응형