
머리말
안드로이드 시스템에서 사용자의 클릭(Touch)을 감지하는 방법은 두가지가 존재한다. 기본적으로 제공되는 TouchListener에 GestureDetector를 사용하느냐 그렇지 않느냐로 구분할 수 있다. 아래의 예제는 touchView, gestureView에서 각각의 모션을 감지하는 셈플이다.
MainActivity.java
package com.example.wookoa.wookoa_11;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView = null;
GestureDetector detector = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 텍스트뷰에 좌표를 찍기위해 생성
textView = (TextView)findViewById(R.id.textView);
// 터치 리스너 생성
View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
int action = event.getAction(); // 터치의 액션 종류를 저장
float currentX = event.getX(); // 사용자가 터치한 X 좌표 값을 저장
float currentY = event.getY(); // 사용자가 터치한 Y 좌표 값을 저장
if(action == MotionEvent.ACTION_DOWN){
textView.append("Touch Down: [" + currentX + " * " + currentY + "]" + "\n");
} else if(action == MotionEvent.ACTION_MOVE){
textView.append("Touch Move: [" + currentX + " * " + currentY + "]" + "\n");
} else if(action == MotionEvent.ACTION_UP){
textView.append("Touch Up: [" + currentX + " * " + currentY + "]" + "\n");
}
return true;
}
});
// 터치 리스너 생성(제스쳐 객체를 사용)
View gestureView = findViewById(R.id.gestureView);
gestureView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
detector.onTouchEvent(motionEvent);
return true;
}
});
// 제스쳐 디덱터를 커스터 마이징
detector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent motionEvent) {
textView.append("Gesture Detector: onDown Call" + "\n");
return true;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
textView.append("Gesture Detector: onShowPress Call" + "\n");
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
textView.append("Gesture Detector: onSingleTapUp Call" + "\n");
return true;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
textView.append("Gesture Detector: onScroll Call [" + v + " * " + v1 + "]" + "\n");
return true;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
textView.append("Gesture Detector: onLongPress Call" + "\n");
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
textView.append("Gesture Detector: onFling Call [" + v + " * " + v1 + "]" + "\n");
return true;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 터치 이벤트를 구현할 VIEW -->
<View
android:id="@+id/touchView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorAccent" />
<!-- 제스쳐 이벤트를 구현할 VIEW -->
<View
android:id="@+id/gestureView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorPrimary" />
<!-- 사용자의 클릭 및 제스쳐에 따른 로그 영역 -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:textSize="16dp" />
</ScrollView>
</LinearLayout>
꼬리말
현재는 안드로이드 앱을 개발하면서 관련된 정보를 얻기가 매우 용이해졌다. 사용자의 터치를 감지하는 방법에 대해서 샘플을 소개한 본 포스팅은 이로써 마무리를 짓도록 한다.
소중한 댓글 (0)