본문 바로가기
App/Android Studio

Android Studio - View, Layout, Activity

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

2022-08-04(33일차)


📃onTouchEvent() 예제

◾ getAction(), getX(), getY() 속성을 이용하여 Action, x, y를 출력한다.

📝MainActivity.java

package com.example.touchlistener;

import androidx.appcompat.app.AppCompatActivity;

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

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) {
        String arr[] = {"UP", "DOWN", "MOVE"};
        int n = event.getAction();
        String strAction = arr[n];

        float x = event.getX();
        float y = event.getY();

        Log.i("jyeon", "Action : " + strAction + "\nx, y : " + x + ", " + y);

        return super.onTouchEvent(event);
    }
}

💡View, Layout, Activity

◾ View - onTouch
◾ Layout - onTouch
◾ Activity - onTouchEvent

🔸순서
◾ View, Layout, Activity Touch

📝MainActivity.java

▪️ 레이아웃 onTouch, 뷰 onTouch

package com.example.ontouchevent;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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

        LinearLayout ll = findViewById(R.id.ll);
        ll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.i("jiyeon", "레이아웃 onTouch" + motionEvent.getAction());
                return true;
            }
        });

        TextView tv = findViewById(R.id.tv);
        tv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.i("jiyeon", "뷰 onTouch" + motionEvent.getAction());
                return false;
            }
        });
    }
}

📝layout.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:id="@+id/ll">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tv"
        android:background="@color/purple_200"/>

</LinearLayout>

📝layout.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"
    android:padding="10dp"
    android:id="@+id/linear">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="1번 이미지 선택"
        android:textSize="50dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img_01"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:src="@drawable/house" />

        <ImageView
            android:id="@+id/img_02"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:src="@drawable/house" />

    </LinearLayout>

</LinearLayout>

📝MainActivity.java

▪️ 레이아웃 onTouch, 이미지01 onTouch, 이미지02 onTouch

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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

        LinearLayout linear = findViewById(R.id.ll);
        linear.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.i("jiyeon", "레이아웃 onTouch" + motionEvent.getAction());
                return false;
            }
        });

        ImageView img_01 = findViewById(R.id.img_01);
        ImageView img_02 = findViewById(R.id.img_02);
        img_01.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.i("jiyeon", "이미지01 onTouch" + motionEvent.getAction());
                return true;
            }
        });

        img_02.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.i("jiyeon", "이미지02 onTouch" + motionEvent.getAction());
                return false;
            }
        });

    }
}

 


 

728x90
반응형

댓글