java 如何在Wear OS应用中捕捉手腕手势?

kcugc4gi  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(91)

我希望看到一个简单、完整的Android Studio Wear OS Java应用源代码,用于处理Wear OS手腕手势。我已经在Text上查看了指南,但希望看到一个完整的示例,包括Java源代码、xml和清单。

oknwwptz

oknwwptz1#

这里是一个演示代码的手腕手势。
MainActivity.java

package com.example.appname;

import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends WearableActivity {

    private TextView mTextView;

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

        mTextView = findViewById(R.id.text);

        // Enables wrist gestures
        setAmbientEnabled();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            // Wrist flick gesture
            if (ev.getEdgeFlags() == MotionEvent.EDGE_WRIGHT) {
                Log.d("Wrist Gesture", "Flick wrist right");
                mTextView.setText("Flicked wrist right");
                return true;
            } else if (ev.getEdgeFlags() == MotionEvent.EDGE_LEFT) {
                // Wrist tilt gesture
                Log.d("Wrist Gesture", "Tilt wrist left");
                mTextView.setText("Tilted wrist left");
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    app:layout_box="all">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="Hello World!" />

</android.support.wearable.view.BoxInsetLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.appname">

    <uses-feature android:name="android.hardware.type.watch" />

    <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/Theme.Wearable">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:exported="true"
            android:theme="@android:style/Theme.DeviceDefault.Light">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="com.google.android.wearable.action.WEARABLE_ACTIVITY" />
            </intent-filter>
        </activity>

    </application>

</manifest>

相关问题