본문 바로가기
App/Android Studio

Android Studio - OnTouchListener 이벤트

by 코젼 2022. 8. 3.
728x90
반응형

2022-08-03(32일차)


💡OnTouchListener 이벤트

◾ 터치(클릭)할 때마다 Log를 찍는다.

📝MainActivity.java

package com.example.touchstudy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        TextView tv = findViewById(R.id.tv);
        tv.setOnTouchListener(lis);
    }

    View.OnTouchListener lis = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            Log.i("jyeon", "onTouch");
            return false;
        }
    };
}

📃onTouchListener 예제 (1)

◾ motionEvent를 getAction()으로 확인할 수 있다.

📝MainActivity.java

package com.example.touchstudy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        TextView tv = findViewById(R.id.tv);
        tv.setOnTouchListener(lis);
    }

    View.OnTouchListener lis = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action = motionEvent.getAction();
            float x = motionEvent.getX();
            float y = motionEvent.getY();

            Log.i("jyeon", "onTouch : action" + action + ", x : " + x + ", y : " + y);
            return true;
        }
    };
}

📃onTouchListener 예제 (2)

📝layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/snow"
        android:id="@+id/iv"/>
</RelativeLayout>

📝MainActivity.java

package com.example.touchstudy;

import androidx.appcompat.app.AppCompatActivity;

import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    String str_action[] = {"DOWN", "UP", "MOVE"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        ImageView iv = findViewById(R.id.iv);
        iv.setOnTouchListener(lis);
    }

    View.OnTouchListener lis = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if(motionEvent.getAction()>=0 && motionEvent.getAction() < str_action.length) {
                Log.i("jyeon", "action : " + str_action[motionEvent.getAction()] + "\n"
                + "x, y position : " + motionEvent.getX() + ", " + motionEvent.getY() + "\n"
                + "event time : " + motionEvent.getEventTime() + "\n"
                + "down event time" + motionEvent.getDownTime());
            }
            return true;
        }
    };
}

📝Logcat


📃onTouchEvent 예제

📝MainActivity.java

package com.example.touchevent;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintSet;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("jyeon", "onTouchEvent");
        return super.onTouchEvent(event);
    }
}

📝Logcat

728x90
반응형

댓글