我想看到一个简单的,完整的Android Studio Wear OS Java应用程序源代码,处理Wear OS手腕手势。我已经审查了文本中的指导,但希望看到一个完整的示例,包括java源代码,xml和清单。
这是一个手腕手势的演示代码。
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>
Build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.appname"
minSdkVersion 25
targetSdkVersion 30
versionCode 1
versionName "1.0"
// Enabling multidex support for the app.
multiDexEnabled true
// Enabling standalone mode for Wear OS apps.
wearAppUnbundled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.wear:wear:1.1.0'
// Adding multidex support.
implementation 'com.android.support:multidex:1.0.3'
}
您还可以使用以下代码来捕获手腕手势:
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent event) {
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
if (event1.getY() - event2.getY() > 100 && Math.abs(velocityY) > 100) {
// Handle up gesture
return true;
} else if (event2.getY() - event1.getY() > 100 && Math.abs(velocityY) > 100) {
// Handle down gesture
return true;
}
return super.onFling(event1, event2, velocityX, velocityY);
}
});