安卓手势创建与保存代码:

MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.example.gesture;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
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.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

public class MainActivity extends Activity implements OnGesturePerformedListener
{

private GestureOverlayView mDrawGestureView;
private static GestureLibrary sStore;

// 权限申请
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public void myPermission() {
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mDrawGestureView = (GestureOverlayView)findViewById(R.id.gesture);

//设置手势可多笔画绘制,默认情况为单笔画绘制
mDrawGestureView.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
//设置手势的颜色(蓝色)
mDrawGestureView.setGestureColor(gestureColor(R.color.gestureColor));
//设置还没未能形成手势绘制是的颜色(红色)
mDrawGestureView.setUncertainGestureColor(gestureColor(R.color.ungestureColor));
//设置手势的粗细
mDrawGestureView.setGestureStrokeWidth(4);
/*手势绘制完成后淡出屏幕的时间间隔,即绘制完手指离开屏幕后相隔多长时间手势从屏幕上消失;
* 可以理解为手势绘制完成手指离开屏幕后到调用onGesturePerformed的时间间隔
* 默认值为420毫秒,这里设置为0.5秒
*/
mDrawGestureView.setFadeOffset(500);

//绑定监听器
mDrawGestureView.addOnGesturePerformedListener(this);
//创建保存手势的手势库
createStore();
}

private void createStore()
{
File mStoreFile = null;
/*判断mStoreFile是为空。
* 判断手机是否插入SD卡,并且应用程序是否具有访问SD卡的权限
*/
if (mStoreFile == null && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
mStoreFile = new File(Environment.getExternalStorageDirectory(), "mygesture");
Log.d("gesture",Environment.getExternalStorageDirectory().getAbsolutePath());
Log.d("gesture","mStore"+mStoreFile.getAbsolutePath());
}



if (sStore == null)
{
/* 另外三种创建保存手势文件的方式如下:
//保存手势的文件在手机SD卡中
sStore = GestureLibraries.fromFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "mygesture");
sStore = GestureLibraries.fromPrivateFile(this, Environment.getExternalStorageDirectory().getAbsolutePath + "mygesture");


*/
//保存手势的文件在应用程序的res/raw文件下
//sStore = GestureLibraries.fromRawResource(this, R.raw.gestures);
sStore = GestureLibraries.fromFile(mStoreFile);

}
testLoad();
}

//测试保存手势的文件是否创建成功
private void testLoad()
{
if (sStore.load())
{
showMessage("手势文件装载成功");
Log.d("gesture","手势文件转载成功");
// 显示所有手势名称
Iterator<String> value;
Log.d("gesture","加载成功");
final Set<String> entries=sStore.getGestureEntries();// 获取所有保存的手势名称
value=entries.iterator();
int i=0;
while (value.hasNext()){
i=i+1;
Log.d("gesture","手势名称:"+value.next()+i);
}


}
else
{
Log.d("gesture","手势文件转载失败");
showMessage("手势文件装载失败");
}
}

public static GestureLibrary getStore()
{
return sStore;
}

//手势绘制完成时调用
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture)
{
// TODO Auto-generated method stub
creatDialog(gesture);
}

private void creatDialog(final Gesture gesture)
{

final View dialogView = getLayoutInflater().inflate(R.layout.show_gesture, null);
//imageView用于显示绘制的手势
ImageView imageView = (ImageView) dialogView.findViewById(R.id.show);

// 调用Gesture的toBitmap方法形成对应手势的位图
final Bitmap bitmap = gesture.toBitmap(128, 128, 10, gestureColor(R.color.showColor));
imageView.setImageBitmap(bitmap);

Builder dialogBuider = new AlertDialog.Builder(MainActivity.this);

dialogBuider.setView(dialogView);
//绑定对话框的确认按钮监听事件
dialogBuider.setPositiveButton(
"保存", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{

// 获取权限
myPermission();
//获取用户保存手势的名字
//final String name = "Star";
EditText editText = (EditText)dialogView.findViewById(R.id.name);
final String name = editText.getText().toString(); //移到确认按钮监听事件里面,不然总是会获得空内容

// 添加手势
sStore.addGesture(name, gesture);
// 保存添加的手势
sStore.save();
Log.v("gesture","save");
}
});
//绑定对话框的取消按钮监听事件
dialogBuider.setNegativeButton("取消", new OnClickListener()
{

@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub

}
});
//显示对话框
dialogBuider.show();
}

private int gestureColor(int resId)
{
return getResources().getColor(resId);
}

private void showMessage(String s)
{
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
}

@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
//移除绑定的监听器
mDrawGestureView.removeOnGesturePerformedListener(this);
}

}

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<android.gesture.GestureOverlayView
android:id="@+id/gesture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" >
</android.gesture.GestureOverlayView>

</androidx.constraintlayout.widget.ConstraintLayout>

show_gesture.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:text="@string/set_gesture_name"/>

<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

<ImageView
android:id="@+id/show"
android:layout_gravity="center"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_marginTop="10dp"/>
</LinearLayout>

生成之后目录在: /storage/emulated/0/mygusture

image-20191126135216632

如果保存之后在这里看不到文件就打开cmd,

输入adb root

然后再来看就出现了

image-20191126135300298