添加识别手势Gesture

来源:互联网 发布:沂星网络机顶盒 编辑:程序博客网 时间:2024/06/10 20:17
package com.example.tyxiong.myapplication;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.gesture.Prediction;import android.graphics.Bitmap;import android.graphics.Color;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;import java.io.IOException;import java.util.ArrayList;/*** Android的数据存储.前面用到PreferencesActivity存储参数设置...shared_文件.*    少数据可用SharedPreferences File来存储;数据量大用SQL,当然手机不是服务器.** SharedPreferences File SD卡 SQLiteDatabase** SharedPreferences:文件存储位置在data/data/<包>.share_.xml  key-value对*  1 SharePreferences,负责读取其内数据3方法;*  2 Editor,SharedPreferences内部接口,负责向其内写数据,4方法(putxxx()/clear()/remove()/commit())**  使用步骤:4步*   1SharedPreferences对象,Context方法getSharedPreferences(name,mode) 获取指定文件名 对象,3模式(private/../..)*   2Editor对象, SharedPreferences对象 edit()方法.*   3Editor对象 写数据.*   4SharedPreferences读.**File存储. java提供了完整的IO流体系.各种流.字节流/字符流/节点流/处理流/转换流/...**         Context有2方法 openFileOutput()4模式(private/append/....)/openFileInput()---文件流.为两方法仅能打开该context应用程序文件夹文件..*         Context有4方法访问该程序文件夹. 获取根下指定子目录/根下所有文件/删除根下指定文件/根绝对路径*         使用步骤4:*           1 Context的openFileOutput(指定模式4种)方法获得指定文件名对应文件的outStream(*           2 可包装成打印流)写入数据. 写完关闭流.*           3 Context的openFileInput()方法获得文件的输入流,*           4 可包装成处理流(字符处理流)读行),读出数据,关闭流(顶端流.)** 读写SD卡文件: SD卡扩展了手机的存储能力. 使用分为4步:*   1 AndroidManifest.xml中添加权限MOUNT_UNMOUNT_FILESYSTEMS(加入删除文件); WRITE_EXTERNAL_STORAGE(写SD卡)*   2 判断是否存在SD卡且有读写权限 Environment.getExternalStorageStage().equals(Environment.MEDIA_MOUNTED)*   3 Environment.getExternalStorageDirectory();获得Sd卡目录,*   4 获取指定文件流入/流出流进行数据操作...new FileOutputStream()  (无法用openFile...那只针对应用程序路径文件)*   存取数据可以用RandomAccessFile...更方便.*** SQLite数据库.:轻量级数据库,底部只一个数据库文件.*   SQLiteDatabase:代表数据库连接.其有4个静态方法用于获取指定文件对应的数据库连接对象.openOrCreate()*   SQL语句分3类:*       DDL(数据库定义语句) 4关键字 create(建表)/drop/alert/truncate*       DML(数据库操作语句) 3关键字 insert(插入记录)/delete/update*       查询语句           1   select**   使用4步骤:*       1 openOrCreate()获取指定文件数据库连接对象..*       2 SQLiteDatabase对象6方法,execSQL():可以执行任何SQL语句(一般是DDL/DML因为没返回值嘛) rawQuery():执行SQL查询语句.*           4个insert(3参数)/delete(3参数)/update(4参数)/query(参数太多用rawQuery()吧)*       3 有查询语句要处理结果集Cursor(可移动记录指针6方法move/next/previous/first/last/),注意用simpleCursorAdapter包装Cursor时,用_id主键列名*       4 关闭数据库连接. close()**   SQLite3 :数据库操作工具,位于平台工具下, 将数据库文件导出到本地 sqlite3 ../fileXxx.db3启动数据库.**   SQLite有5个数据类型,null,int,real(浮点),text文本,blob(大二进制)*       可将任何类型数据存入到任何类型字段中.*       建表时列名后的数据类型可省略.*       例外:定义为INTEGER PRIMARY KEY的字段只能存储64位整数.**   占位符 ?通配符 %多个字符 _单个字符**   事务:4个方法 beginTransaction()/inTransaction()/setTransactionSuccessful()/endTransaction()*   判断是否提交事务/回滚事务若调用了setTransactionSucessful()就提交.**   SQLiteOpenHelper类,管理数据库的创建和更新.5个方法onCreate()/onUpdate()/getReadableDatabase()/getWriteableDatabase()/close()*   onCreate()首次创建数据库时回调方法,方法里可建新表.*   onUpdate()当数据库版本更新时回调*   getReadableDatabase()读写方式打开文件对应数据库连接对象(SQLiteDatabase)*   getWriteableDatabase()写方式.....*   close()关闭所有数据库连接.**   使用步骤3步:*       1 继承SQLiteOpenHelper类,重写onCreate()方法(内建表),重写ouUpdate()方法*       2 调用getReadableDatabase()获取数据库连接对象.*       3 数据库对象执行SQL语句..*       4 close()关闭数据库连接.*  DDL create table <表名>(列名1 列类型 主键列/限定,列名2 类型...);*  DML insert into table<表名>(列名,) values(值)  /value(值1,null,值2);*  查询语句 select * from table<表名> where cause;***手势Gesture 1连续的触摸屏点形成趋势,为它们提供了监听. 2形成形状,可添加为手势保存还可识别手势.*   手势检测2步:*       1 new GestureDetector()/需要传入OnGestureListener*   监听器有(6方法2按下onDown()/onShowPress() /1拖动OnFling() /1滚动OnScroll()/1长按onLongPress()/1轻击(onSingleTapup()))*       2 重写Activity的onTouchEvent()方法,将该Activity上的触摸事件交给GestureDetector处理*   * 增加手势: GestureLibrary手势库 GestureLibraries手势库工具类(从不同路径获取手势库) GestureOverlayView绘制手势组件   * GestureLibrary有7方法,1addGesture()增加手势 2删除手势 2查询手势 1识别手势Recognise() 1保存手势save()   * GestureLibraries4方法 从不同路径获取手势库.   * GestureOverlayView 绘制手势的组件(xml中加组件全包名)3个监听器 开始绘制/正在绘制/完成绘制   使用3步:        1 GestureLibraries 通过fromFile..()方法获取手势库对象.   *    2 为GestureOverLayView组件,添加绘制完成监听   *    3 绘制完成,调用GestureLibrary的addGesture() save()添加手势并保存.   *   * 识别手势:手势库有方法recognise()   *   * 自动朗读TTS(Text To Speech)    使用4步:        1 new TextToSpeech()对象,需要一个监听器参数(监听TextToSpeech对象初始化状态,实现ini方法,初始完对象回调)        2 设置语言(不支持中文) setLanguage() 如Locale.UK        3 speak()方法朗读 有模式新朗读/追加        4 shutdown()关闭TextToSpeech**以下演示...* 增加手势: GestureLibrary手势库 GestureLibraries手势库工具类(从不同路径获取手势库) GestureOverlayView绘制手势组件   * GestureLibrary有7方法,1addGesture()增加手势 2删除手势 2查询手势 1识别手势Recognise() 1保存手势save()   * GestureLibraries4方法 从不同路径获取手势库.   * GestureOverlayView 绘制手势的组件(xml中加组件全包名)3个监听器 开始绘制/正在绘制/完成绘制   使用3步:   *    1 为GestureOverLayView组件,添加绘制完成监听        2 GestureLibraries 通过fromFile..()方法获取手势库对象.   *    3 绘制完成,调用GestureLibrary的addGesture() save()添加手势并保存.   *   * 识别手势:手势库有方法recognise()* */public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        GestureOverlayView gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureView);        gestureOverlayView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {            @Override            public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {                final View view = getLayoutInflater().inflate(R.layout.alert_view, null);                final ImageView imageView = (ImageView) view.findViewById(R.id.imageView);                final EditText editText = (EditText) view.findViewById(R.id.textView);                Bitmap bitmap = gesture.toBitmap(overlay.getWidth(), overlay.getHeight(), 10, Color.YELLOW);                imageView.setImageBitmap(bitmap);                new AlertDialog.Builder(MainActivity.this)                        .setTitle("保存手势")                        .setView(view)                        .setPositiveButton("保存", new DialogInterface.OnClickListener() {                            public StringBuffer sb = new StringBuffer();                            public double dscore;                            public String MY_GESTURE = "/gesture";                            @Override                            public void onClick(DialogInterface dialog, int which) {                                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {                                    try {                                        GestureLibrary gestureLibrary = GestureLibraries.fromFile(Environment.getExternalStorageDirectory().getCanonicalPath() + MY_GESTURE);                                        gestureLibrary.load();//加载该手势库里的手势. 如若不加载,直接向里添加手势addGesture()会将原来的手势覆盖.                                        gestureLibrary.getGestureEntries();                                        gestureLibrary.addGesture(editText.getText().toString(), gesture);//                                        gestureLibrary.save();                                        ArrayList<Prediction> list = gestureLibrary.recognize(gesture);                                        for (Prediction pre : list                                                ) {                                            dscore = pre.score;                                            sb.append(String.valueOf(dscore));                                        }                                        Toast.makeText(MainActivity.this, sb, Toast.LENGTH_SHORT).show();                                    } catch (IOException e) {                                        e.printStackTrace();                                    }                                }                            }                        }).setNegativeButton("取消", null).create().show();            }        });    }}<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/linearLay"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:id="@+id/write"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:onClick="write"        android:text="写入" />    <Button        android:id="@+id/read"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:text="读取"        android:onClick="read"/><android.gesture.GestureOverlayView    android:layout_height="match_parent"    android:layout_width="match_parent"    android:id="@+id/gestureView"    android:gestureStrokeType="multiple"    /></LinearLayout><?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"    >    <EditText        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="Input gestureName..."        android:id="@+id/textView" />    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/imageView" /></LinearLayout><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    package="com.example.tyxiong.myapplication">    <!--修改 删除文件 写SD卡权限-->    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@android:style/Theme.Holo.Light"        tools:ignore="AllowBackup,GoogleAppIndexingWarning">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
0 0