이 글에서는 안드로이드에서 ACTION_MOVE 이벤트, 즉 화면을 터치한 상태로 손가락을 움직일 때 발생하는 이벤트를 처리하는 방법을 단계별로 알아보겠습니다.
1단계: 새 프로젝트 생성
안드로이드 스튜디오를 실행하고 File → New Project 메뉴로 이동한 뒤, 새 프로젝트 생성에 필요한 모든 정보를 입력하여 프로젝트를 만듭니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/actionEvent"
android:textSize="40sp"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>위 코드에서는 터치 이동 이벤트를 감지할 대상으로 하나의 TextView를 배치했습니다. LinearLayout의 gravity 속성을 center로 지정해 화면 중앙에 정렬되도록 구성했습니다.
3단계: MainActivity 코드 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다.
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView textView;
@SuppressLint({"RestrictedApi", "ClickableViewAccessibility"})
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView actionEvent = findViewById(R.id.actionEvent);
actionEvent.setText("Action Move");
actionEvent.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("TAG", "touched down");
break;
case MotionEvent.ACTION_MOVE:
Toast.makeText(MainActivity.this,"moving: (" + x + "," + y +")",Toast.LENGTH_LONG).show();
break;
case MotionEvent.ACTION_UP:
Log.i("TAG", "touched up");
break;
}
return true;
}
});
}
}코드 설명
핵심 로직은 setOnTouchListener() 메서드에 있습니다. onTouch() 콜백에서는 MotionEvent 객체를 통해 현재 터치 좌표(x, y)를 가져오고, getAction() 값에 따라 다음과 같이 분기 처리합니다.
- ACTION_DOWN: 손가락이 화면에 처음 닿는 순간으로, Logcat에 'touched down' 메시지를 기록합니다.
- ACTION_MOVE: 터치 상태에서 손가락이 움직일 때마다 호출되며, 현재 좌표를 Toast 메시지로 화면에 표시합니다.
- ACTION_UP: 손가락을 화면에서 떼는 순간으로, Logcat에 'touched up' 메시지를 기록합니다.
onTouch() 메서드가 true를 반환하면 해당 뷰가 터치 이벤트를 소비했다는 의미이며, 이후의 MOVE나 UP 이벤트도 계속 전달받을 수 있습니다.
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기가 컴퓨터에 연결되어 있다고 가정합니다. 안드로이드 스튜디오에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run 아이콘을 클릭하세요. 실행할 기기 목록에서 자신의 모바일 기기를 선택하면, 해당 기기에 아래와 같은 기본 화면이 표시됩니다.

화면의 텍스트 영역을 터치한 상태로 손가락을 움직이면, 이동할 때마다 현재 좌표가 담긴 Toast 메시지가 계속 나타나는 것을 확인할 수 있습니다. 이처럼 ACTION_MOVE 이벤트를 활용하면 드래그, 슬라이드 등 다양한 터치 기반 인터랙션을 구현할 수 있습니다.