Android 手势相关(二)

Android 手势相关(二)

本篇文章继续记录下android 手势相关的内容.

1: GestureOverlayView简介

GestureOverlayView是Android中的一个视图组件,用于捕捉和处理手势操作.

GestureOverlayView的主要用途:

  1. 手势识别: 通过GestureOverlayView,保存一些手势,并堆用户手势操作进行识别匹配.
  2. 手势绘制: 我们还可以在GestureOverlayView绘制,并保存绘制路径或者手势.
  3. 手势交互: 我们可以监听手势的开始,结束等事件.

本文主要介绍的是手势识别这块,实现的效果就是设置手势的名称, 保存手势, 绘制手势判断是否匹配.

2: 布局

界面布局主要有几块:

  1. EditText 用于设置手势名称
  2. btn1用于设置保存手势动作
  3. btn2用于设置匹配手势动作
  4. GestureOverlayView用于手势绘制.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GestureActivity">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:hint="请输入保存手势的名称"
        android:id="@+id/edit_name"
        />
    <Button
        android:id="@+id/btn_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存手势"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edit_name"
        />

    <Button
        android:id="@+id/btn_compare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="匹配手势"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_save" />

    <android.gesture.GestureOverlayView
        android:id="@+id/gesture"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="#aaaaaa"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_compare" />
</androidx.constraintlayout.widget.ConstraintLayout>

GestureOverlayView在xml定义的属性可以查看下attrs.xml.

<!-- GestureOverlayView specific attributes. These attributes are used to configure
     a GestureOverlayView from XML. -->
<declare-styleable name="GestureOverlayView">
    <!-- Width of the stroke used to draw the gesture. -->
    <attr name="gestureStrokeWidth" format="float" />
    <!-- Color used to draw a gesture. -->
    <attr name="gestureColor" format="color" />
    <!-- Color used to draw the user's strokes until we are sure it's a gesture. -->
    <attr name="uncertainGestureColor" format="color" />
    <!-- Time, in milliseconds, to wait before the gesture fades out after the user
         is done drawing it. -->
    <attr name="fadeOffset" format="integer" />
    <!-- Duration, in milliseconds, of the fade out effect after the user is done
         drawing a gesture. -->
    <attr name="fadeDuration" format="integer" />
    <!-- Defines the type of strokes that define a gesture. -->
    <attr name="gestureStrokeType">
        <!-- A gesture is made of only one stroke. -->
        <enum name="single" value="0" />
        <!-- A gesture is made of multiple strokes. -->
        <enum name="multiple" value="1" />
    </attr>
    <!-- Minimum length of a stroke before it is recognized as a gesture. -->
    <attr name="gestureStrokeLengthThreshold" format="float" />
    <!-- Squareness threshold of a stroke before it is recognized as a gesture. -->
    <attr name="gestureStrokeSquarenessThreshold" format="float" />
    <!-- Minimum curve angle a stroke must contain before it is recognized as a gesture. -->
    <attr name="gestureStrokeAngleThreshold" format="float" />
    <!-- Defines whether the overlay should intercept the motion events when a gesture
         is recognized. -->
    <attr name="eventsInterceptionEnabled" format="boolean" />
    <!-- Defines whether the gesture will automatically fade out after being recognized. -->
    <attr name="fadeEnabled" format="boolean" />
    <!-- Indicates whether horizontal (when the orientation is vertical) or vertical
         (when orientation is horizontal) strokes automatically define a gesture. -->
    <attr name="orientation" />
</declare-styleable>

这里我只用到了gestureStrokeWidth,gestureColor两个属性,并且是在代码中设置的.

3: GestureLibrary对象

GestureLibrary 是android 中用于保存和管理手势的类. 源码很简单,具体的方法有兴趣的都去试下

public abstract class GestureLibrary {
    protected final GestureStore mStore;

    protected GestureLibrary() {
        mStore = new GestureStore();
    }

    public abstract boolean save();

    public abstract boolean load();

    public boolean isReadOnly() {
        return false;
    }

    /** @hide */
    public Learner getLearner() {
        return mStore.getLearner();
    }

    public void setOrientationStyle(int style) {
        mStore.setOrientationStyle(style);
    }

    public int getOrientationStyle() {
        return mStore.getOrientationStyle();
    }

    public void setSequenceType(int type) {
        mStore.setSequenceType(type);
    }

    public int getSequenceType() {
        return mStore.getSequenceType();
    }

    public Set<String> getGestureEntries() {
        return mStore.getGestureEntries();
    }

    public ArrayList<Prediction> recognize(Gesture gesture) {
        return mStore.recognize(gesture);
    }

    public void addGesture(String entryName, Gesture gesture) {
        mStore.addGesture(entryName, gesture);
    }

    public void removeGesture(String entryName, Gesture gesture) {
        mStore.removeGesture(entryName, gesture);
    }

    public void removeEntry(String entryName) {
        mStore.removeEntry(entryName);
    }

    public ArrayList<Gesture> getGestures(String entryName) {
        return mStore.getGestures(entryName);
    }
}

创建对象:

gestureLibrary = GestureLibraries.fromFile("sdcard/gesture");

根据路径创建gestureLibrary创建对象. 这里无需关注文件是否存在,save方法会有判断:

public boolean save() {
    if (!mStore.hasChanged()) return true;

    final File file = mPath;

    final File parentFile = file.getParentFile();
    if (!parentFile.exists()) {
        if (!parentFile.mkdirs()) {
            return false;
        }
    }

    boolean result = false;
    try {
        //noinspection ResultOfMethodCallIgnored
        file.createNewFile();
        mStore.save(new FileOutputStream(file), true);
        result = true;
    } catch (FileNotFoundException e) {
        Log.d(LOG_TAG, "Could not save the gesture library in " + mPath, e);
    } catch (IOException e) {
        Log.d(LOG_TAG, "Could not save the gesture library in " + mPath, e);
    }

    return result;
}

由于是文件操作,权限不能忘记:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

4: GestureOverlayView

GestureOverlayView提供了三个接口实现:

  1. addOnGestureListener(OnGestureListener listener)
  2. addOnGesturePerformedListener(OnGesturePerformedListener listener)
  3. addOnGesturingListener(OnGesturingListener listener)

这里我们使用addOnGesturePerformedListener,将手势识别器与手势监听器关联:

gestureOverlayView.setGestureColor(R.color.teal_200);
gestureOverlayView.setGestureStrokeWidth(5);
gestureOverlayView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
 		  //处理手势执行 
    }
}

onGesturePerformed方法中我们做两个操作:

  1. 保存手势
  2. 判断手势匹配

保存手势的操作:

根据onGesturePerformed回调的gesture,我们调用gestureLibrary.addGesture添加手势,并调用gestureLibrary.save()进行保存.

gestureLibrary.addGesture(editText.getText().toString(), gesture);
if (gestureLibrary.save()) {
    Toast.makeText(GestureActivity.this, "保存手势成功", Toast.LENGTH_LONG).show();
}else{
    Toast.makeText(GestureActivity.this, "保存手势失败", Toast.LENGTH_LONG).show();
}

判断匹配:

调用recognize方法,传入一个手势参数,返回与参数手势最匹配的手势,

Prediction.score是用来表示手势匹配的置信度或相似度的指标,

它是一个浮点数,范围通常是0到10之间,表示匹配的程度.

ArrayList<Prediction> recognize = gestureLibrary.recognize(gesture);
Prediction prediction = recognize.get(0);
if (prediction.score >= 2) {
    Toast.makeText(GestureActivity.this, prediction.name + "匹配成功", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(GestureActivity.this, prediction.name + "匹配失败", Toast.LENGTH_SHORT).show();
}

完整的代码如下:

public class GestureActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "GestureActivity";
    private GestureOverlayView gestureOverlayView;
    private Button btnSave, btnCompare;
    private int status = 0; // 1:保存手势 2: 手势比较
    private GestureLibrary gestureLibrary;
    private EditText editText;

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture);
        gestureOverlayView = findViewById(R.id.gesture);
        btnSave = findViewById(R.id.btn_save);
        btnCompare = findViewById(R.id.btn_compare);
        editText = findViewById(R.id.edit_name);
        gestureLibrary = GestureLibraries.fromFile("sdcard/gesture");
        btnSave.setOnClickListener(this);
        btnCompare.setOnClickListener(this);
        gestureOverlayView.setGestureColor(R.color.teal_200);
        gestureOverlayView.setGestureStrokeWidth(5);
        gestureOverlayView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                if (status == 1) {//保存手势
                    if (gesture == null || gesture.getLength() <= 0) return;
                    if (TextUtils.isEmpty(editText.getText().toString())) {
                        Toast.makeText(GestureActivity.this, "请输入手势名称", Toast.LENGTH_LONG).show();
                        return;
                    }
                    gestureLibrary.addGesture(editText.getText().toString(), gesture);
                    if (gestureLibrary.save()) {
                        Toast.makeText(GestureActivity.this, "保存手势成功", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(GestureActivity.this, "保存手势失败", Toast.LENGTH_LONG).show();
                    }
                } else if (status == 2) {//比较手势
                    ArrayList<Prediction> recognize = gestureLibrary.recognize(gesture);
                    Prediction prediction = recognize.get(0);
                    if (prediction.score >= 2) {
                        Toast.makeText(GestureActivity.this, prediction.name + "匹配成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(GestureActivity.this, prediction.name + "匹配失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_compare:
                status = 2;
                break;
            case R.id.btn_save:
                status = 1;
                break;
        }
    }
}

本文由博客一文多发平台 OpenWrite 发布!

热门相关:至尊凰妃   明天和意外   福慧双全   剑道邪尊   嫁入豪门后我养崽盘大佬