Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีตรวจจับท่าทาง Fling (Swipe) ในแอป Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการตรวจจับท่าทาง Fling (Swipe/ปัดหน้าจอ) ใน Android โดยใช้ GestureDetector และ SimpleOnGestureListener

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio → File → New Project → กรอกรายละเอียดที่จำเป็น

ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="ลองสวายหน้าจอ"
        android:textStyle="bold" />

</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity (src/MainActivity.java)

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    GestureDetector gestureDetector;

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

        gestureDetector = new GestureDetector(this, new GestureListener());
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Toast.makeText(MainActivity.this, "ตรวจจับ Fling!", Toast.LENGTH_SHORT).show();
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // ส่งสัญญาณ touch ไปยัง GestureDetector
        gestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
}

ขั้นตอนที่ 4: กำหนดค่า AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

เมธอดอื่นๆ ของ SimpleOnGestureListener

เมธอด คำอธิบาย
onDown(MotionEvent e) กดลง (ต้อง return true เพื่อรับ event ต่อ)
onSingleTapUp(MotionEvent e) แตะเดี่ยว
onDoubleTap(MotionEvent e) แตะสองครั้ง
onLongPress(MotionEvent e) กดค้าง
onScroll(MotionEvent e1, e2, dx, dy) ลาก/เลื่อน (Drag/Scroll)
onFling(MotionEvent e1, e2, vX, vY) ปัด/สวายเร็ว (Fling/Swipe)

การคำนวณทิศทาง Fling

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    float diffX = e2.getX() - e1.getX();
    float diffY = e2.getY() - e1.getY();

    if (Math.abs(diffX) > Math.abs(diffY)) {
        // แนวนอน
        if (diffX > 0) {
            // สวายขวา
        } else {
            // สวายซ้าย
        }
    } else {
        // แนวตั้ง
        if (diffY > 0) {
            // สวายลง
        } else {
            // สวายขึ้น
        }
    }
    return true;
}

การทดสอบ

รันแอปบนอุปกรณ์จริง สวายหน้าจอเร็ว จะแสดง Toast "ตรวจจับ Fling!"

วิธีตรวจจับท่าทาง Fling (Swipe) ในแอป Android