GestureLibrary增加手势笔记
界面布局:主界面
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:layout_height="wrap_content"
android:text="请在下面屏幕上绘制手势"
/>
android:id="@+id/gesture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gestureStrokeType="multiple"
>
saveDialog界面:
<?xml version="1.0" encoding="utf-8"?>
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:text="手势"
/>
>
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_marginTop="10dp"
/>
package com.example.gesturetest4;
/*
步骤:1.主布局内必有android:gesture.GestureOverlayView(手势绘制组件),其中一个参数
为 android:gestureStrokeType 可设置手势是一笔还是多笔,单笔 single ,多笔 :multiple ,另可setGestureColor ,setGestureWidth()设置笔画颜色,宽度 。* 2.OnGesturePerformedListener监听器,用户在绘制手势完成后,步骤1的组件.addOnGesturePerformedListener()将对用户
* 的手势加工处理,得到结果。
* 3.文章为android设置对话框处理用户的手势。
* 4.利用GestureLibraries 获取指定文件的手势库(GestureLibrary),通过GestureLibrary内部方法对其增删改操作。
*
*/
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
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.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText editText;
//创建手势编辑器 ,手势编辑器
GestureOverlayView gestureView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取手势编辑视图
gestureView=(GestureOverlayView)findViewById(R.id.gesture);
//设置手势颜色
gestureView.setGestureColor(Color.RED);
//设置手势的绘制宽度
gestureView.setGestureStrokeWidth(4);
gestureView.addOnGesturePerformedListener(new OnGesturePerformedListener(){
@Override
//final Gesture gesture此处参数为用户已画好的 gesture手势
public void onGesturePerformed(GestureOverlayView oberlay, final Gesture gesture) {
// TODO Auto-generated method stub
//将编辑视图saveDialog .xml文件转为View ,此为标准获取视图方法,牢记
View saveDialog=getLayoutInflater().inflate(R.layout.savedialog, null);
//在save视图内定义图片框,显示手势
ImageView imageView=(ImageView)saveDialog.findViewById(R.id.show);
//定义一个文本框,可以显示客户将要保存文件的文件名,作为EntryName
final EditText gestureName=(EditText)saveDialog.findViewById(R.id.gesture_name);
//定义文本框,手势转为位图
Bitmap bitmap=gesture.toBitmap(128,128,10,0xffff0000);
//将位图通过ImageView显示出来
imageView.setImageBitmap(bitmap);
//定义一个对话框,显示视图saveView ,并设置点击保存,和取消按钮的后续操作
new AlertDialog.Builder(MainActivity.this).setView(saveDialog).setPositiveButton("保存", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//取得手势库
GestureLibrary gestureLib=GestureLibraries.fromFile("/mnt/sdcard/mygestures");
//添加手势
gestureLib.addGesture(gestureName.getText().toString(), gesture);
//保存
gestureLib.save();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
更多相关文章
- android基本程序单元Activity
- Android(安卓)windowSoftInputMode属性
- android 中文api (63) —— SimpleAdapter.ViewBinder
- Android之记住密码与自动登陆实现
- Android之BroadcastReceiver的使用
- Android(安卓)permission 权限类及中英文说明
- Android(安卓)API 中文(77)――AdapterView.OnItemSelectedListene
- Android(安卓)ViewTreeObserver简介
- android (13) Fragment使用下