使用GestureLibraries管理手势库,GestureLibrary添加删除手势
activity_main.xml
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gesture_main"
android:gestureStrokeWidth="4"
android:gestureColor="#00f"
android:gestureStrokeType="multiple"/>
activity_show_gesture.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:stretchColumns="1"
tools:context="practise.lxm.hello.ShowGesture">
<TableRow android:padding="5dp">
<TextView android:text="手势名称:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/edTxt_show_gestureName"/>
</TableRow>
<TableRow android:padding="5dp">
<TextView android:text="手势"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_height="200dp"
android:layout_width="200dp"
android:background="#0f0"
android:id="@+id/iv_show_gesture"/>
</TableRow>
</TableLayout>
MainActivity.java
package practise.lxm.hello;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
/**
*
*/
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GestureOverlayView gestureOverlayView = (GestureOverlayView)findViewById(R.id.gesture_main);
//手势完成后执行
gestureOverlayView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView overlay,final Gesture gesture) { //因为内部类中使用了gesture,所以需要将手势声明为final
//加载显示手势的Activity
View viewShow = getLayoutInflater().inflate(R.layout.activity_show_gesture,null);
//加载显示Activity里面的 图片视图和编辑视图
ImageView ivGesture = (ImageView)viewShow.findViewById(R.id.iv_show_gesture);
final EditText edTxtGestureName = (EditText)viewShow.findViewById(R.id.edTxt_show_gestureName);
//根据手势生成图片
int strokeWidth = 20; //线条粗细
int strokeColor = Color.RED; //线条颜色
Bitmap bitmap = gesture.toBitmap(ivGesture.getLayoutParams().width,
ivGesture.getLayoutParams().height,strokeWidth, strokeColor);
ivGesture.setImageBitmap(bitmap);
//弹出视图确认是否保存
new AlertDialog.Builder(MainActivity.this)
.setView(viewShow)
.setPositiveButton("保存", new DialogInterface.OnClickListener() { //单击保存
@Override
public void onClick(DialogInterface dialog, int which) {
//获得本地的手势库
GestureLibrary gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture");
//添加手势
gestureLibrary.addGesture(edTxtGestureName.getText().toString(),gesture);
//保存
gestureLibrary.save();
}
})
.setNegativeButton("取消",null) //设置取消按钮
.show(); //显示
}
});
}
}