투케이2K

390. (android/java) setOnTouchListener MotionEvent 사용해 디스플레이 터치 화면 X , Y 좌표 값 확인 실시 본문

Android

390. (android/java) setOnTouchListener MotionEvent 사용해 디스플레이 터치 화면 X , Y 좌표 값 확인 실시

투케이2K 2022. 11. 4. 18:21
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[Java : 소스 코드]

		// ==========================================

		// [로직 처리 실시]
		try {

			// [컴포넌트 매핑 실시]
			LinearLayout parent = (LinearLayout)findViewById(R.id.parent);

			// [터치 이벤트 리스너 정의]
			parent.setOnTouchListener(new View.OnTouchListener() {
				public boolean onTouch(View v, MotionEvent event) {
					switch(event.getAction()) {
						// [누름]
						case MotionEvent.ACTION_DOWN :
							break;
						// [이동]
						case MotionEvent.ACTION_MOVE :
							break;
						// [올림]
						case MotionEvent.ACTION_UP :
							Log.i("---","---");
							Log.d("//===========//","================================================");
							Log.i("","\n"+"[Test_Java > onCreate() 메소드 : 디스플레이 터치 좌표 값 확인]");
							Log.i("","\n"+"[X 좌표 : "+String.valueOf(event.getX())+"]");
							Log.i("","\n"+"[Y 좌표 : "+String.valueOf(event.getY())+"]");
							Log.d("//===========//","================================================");
							Log.i("---","---");
					}
					return true;
				}
			});
		}
		catch (Exception e){
			e.printStackTrace();
		}

		// ==========================================
 

[Xml : 소스 코드]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true"

    android:id="@+id/parent">

</LinearLayout>
 

[결과 출력]


 

반응형
Comments