安卓手势创建与保存代码:
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) { ActivityCompat.requestPermissions( this , PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE ); } } @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 ); mDrawGestureView.setFadeOffset(500 ); mDrawGestureView.addOnGesturePerformedListener(this ); createStore(); } private void createStore () { File mStoreFile = null ; 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 ) { 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) { creatDialog(gesture); } private void creatDialog (final Gesture gesture) { final View dialogView = getLayoutInflater().inflate(R.layout.show_gesture, null ); ImageView imageView = (ImageView) dialogView.findViewById(R.id.show); 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(); 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) { } }); 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 () { 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
如果保存之后在这里看不到文件就打开cmd,
输入adb root
然后再来看就出现了