Android implements custom gesture and gesture recognition functions
- 2021-11-13 18:03:08
- OfStack
1. Complete the Activity of custom gestures first
1.1 You need to declare permissions because you need to store gesture files:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> // Read SD Card authority
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> // Write SD Card authority
1.2 Simply write a layout file, which uses GestureOverlayView, equivalent to a drawing component. There is an important attribute gestureStrokeType. When the value is single, it means that only one stroke is drawn. If you want to draw multiple strokes, the value should be set to multiple:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".addgesture.Main3Activity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="recognition"
android:text=" Recognize gestures " />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=" Please draw a gesture " />
<android.gesture.GestureOverlayView
android:id="@+id/activity_main3_gov"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeType="multiple" // Multi-stroke rendering
></android.gesture.GestureOverlayView>
</LinearLayout>
1.3 The style of AlertDialog is customized here;
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text=" Please enter a gesture name " />
<EditText // Enter a name for the gesture
android:id="@+id/save_dialog_et"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<ImageView // Show the drawn gestures
android:id="@+id/save_dialog_iv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
1.4 Code section:
package com.example.mygesture.addgesture;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.mygesture.R;
import com.example.mygesture.recognitiongesture.Main4Activity;
public class Main3Activity extends AppCompatActivity {
GestureOverlayView gov; // Defining Drawing Components
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} // Higher versions need to apply for permissions dynamically
init();
}
private void init() {
gov = findViewById(R.id.activity_main3_gov);
// gov.setGestureColor(Color.RED); // Set the color of drawing
gov.setGestureStrokeWidth(4); // Set the width of the brush
gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() { // Set drawing completion monitoring
@Override
public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
View saveDialog = getLayoutInflater().inflate(R.layout.save_dialog, null); // Get AlertDialog Layout style of
final EditText editText = saveDialog.findViewById(R.id.save_dialog_et);
ImageView imageView = saveDialog.findViewById(R.id.save_dialog_iv);
Bitmap bitmap = gesture.toBitmap(128, 128, 10, 0xFFFF0000); // Convert gestures to bitmaps
imageView.setImageBitmap(bitmap); // Use ImageView Load gesture pictures
new AlertDialog.Builder(Main3Activity.this).setView(saveDialog).setPositiveButton(" Determine ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
GestureLibrary gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture");// Using gesture library to obtain the address of storing gesture files
gestureLibrary.addGesture(editText.getText().toString(), gesture); // Add gesture names and gestures to the gesture library
gestureLibrary.save(); // Save Gesture Library
Toast.makeText(Main3Activity.this, " Save successfully ", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton(" Cancel ", null)
.show();
}
});
}
public void recognition(View view) {
Intent intent = new Intent(this, Main4Activity.class);
startActivity(intent);
}
}
2. Next, complete the Activity for recognizing gestures:
Write-first layout file like 2.1 1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".recognitiongesture.Main4Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=" Please draw the gesture to be recognized " />
<android.gesture.GestureOverlayView
android:id="@+id/activity_main4_gov"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.gesture.GestureOverlayView>
</LinearLayout>
2.2 Code writing
package com.example.mygesture.recognitiongesture;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.example.mygesture.R;
import java.util.ArrayList;
import java.util.logging.Level;
public class Main4Activity extends AppCompatActivity {
GestureOverlayView gov;
GestureLibrary gestureLibrary; // Defining a Gesture Library
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
init();
}
private void init() {
gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture"); // Getting a Gesture File
if (gestureLibrary.load()) { // Judge whether the gesture file exists and load it
Toast.makeText(this, " Gesture file loaded successfully ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, " Gesture file failed to load ", Toast.LENGTH_SHORT).show();
}
gov = findViewById(R.id.activity_main4_gov);
gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLibrary.recognize(gesture); // Match all gestures in the gesture library
ArrayList<String> result = new ArrayList<>(); // Match result array
for (Prediction pred : predictions) {
if (pred.score > 2) { // Matches all gestures in the gesture library and sets the similarity >2 Save the matching results array
result.add(" Similarity: " + pred.score);
}
}
if (result.size() > 0) { // The adapter is used here as the AlertDialog To display the similarity of all gestures
ArrayAdapter<Object> arrayAdapter = new ArrayAdapter<Object>(Main4Activity.this, android.R.layout.simple_dropdown_item_1line, result.toArray());
new AlertDialog.Builder(Main4Activity.this).setAdapter(arrayAdapter, null).setPositiveButton(" Determine ", null).show();
} else {
Toast.makeText(Main4Activity.this, " No matching gesture was found ", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Summarize