Android学习笔记_28_手势识别

一、准备手势库:

  使用SDK自带例子GestureBuilder建立手势库(位置:android-sdk-windows\samples\android-10\GestureBuilder)。使用GestureBuilder之前,你需要恢复其到开发环境,将其他正确项目下的".classpath",".project"和"project.properties"三个文件拷贝到GestureBuilder项目下,导入到开发环境,然后进行编绎并部署到手机上。此时,就可以使用GestureBuilder建立手势库,生成的手势库文件在SCDard上,默认文件名称为:gestures。

二、开发:

  1、配置文件:

  在配置文件中加入拨打电话权限,后面用到打电话。

<!-- 拨打电话  -->
    <uses-permission android:name="android.permission.CALL_PHONE"/>

  布局文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <!--
        gestureStrokeType:可以设置单笔识别或多笔识别
        layout_weight:优先级别默认为0,最高为0,次之1...。显示界面时,先测量按钮的高度,然后再用窗口高度减去按钮高度,所得的高度设置到手势界面
    -->
    <android.gesture.GestureOverlayView
        android:id="@+id/gestures"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/btnCheck"
        android:layout_weight="1"
        android:gestureStrokeType="multiple" />
   <Button
        android:id="@+id/btnCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_weight="0"
        android:onClick="match"
        android:text="@string/check" />
</RelativeLayout>

  2、后台代码:

package com.example.gesture;

import java.util.List;

import android.R.anim;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.Prediction;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    private static final String TAG="Gesture";
    private GestureOverlayView gestureOverlayView;
    private GestureLibrary mLibrary;
    private boolean state;
    private Gesture gesture;//用户最终画完手势

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        state = mLibrary.load();//加载手势库
        Log.i(TAG, state+"");
        gestureOverlayView = (GestureOverlayView)this.findViewById(R.id.gestures);
        //当用户完成一次Gesture绘制后,系统将自动调用Listener对象的onGesturePerformed()方法,只支持单笔手势
        //gestureOverlayView.addOnGesturePerformedListener(new GestureListener());
        //可以监听单笔和多笔识别
        gestureOverlayView.addOnGestureListener(new MyGestureListener());
    }
    
    public void match(View v){
        matchGesture(gesture);
        gestureOverlayView.clear(true);
    }
    //单多笔监视
    private final class MyGestureListener implements OnGestureListener{
        @Override
        public void onGesture(GestureOverlayView overlay, MotionEvent event) {
             Log.i(TAG, "onGesture() ... ");
        }
        @Override
        public void onGestureCancelled(GestureOverlayView overlay,
                MotionEvent event) {
            Log.i(TAG, "onGestureCancelled() ... ");
        }
        @Override
        public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
            Log.i(TAG, "onGestureEnded() ... ");
            gesture=overlay.getGesture();
        }
        @Override
        public void onGestureStarted(GestureOverlayView overlay,
                MotionEvent event) {
            Log.i(TAG, "onGestureStarted() ... ");
        }
    }
    
    //单笔手势监听类
    private final class GestureListener implements GestureOverlayView.OnGesturePerformedListener{
        @Override
        public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
            if(state){
                matchGesture(gesture);
            }
        }
    }
    private void matchGesture(Gesture gesture) {
        //123:对号,258:X,369:李,456:Z
        //从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度高的结果放在最前面
        List<Prediction> predictions = mLibrary.recognize(gesture);
        if(!predictions.isEmpty()){
            Prediction prediction = predictions.get(0);
            //prediction的score属性代表了与手势的相似程度
            //prediction的name代表手势对应的名称
            if(prediction.score > 6){
                if("123".equals(prediction.name)){
                    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:159016"));
                    startActivity(intent);
                }else if ("456".equals(prediction.name)) {
                    finish();//关闭activity,会触发onDestroy方法,进而关闭应用
                }else if ("369".equals(prediction.name)) {
                    Toast.makeText(getApplicationContext(), "结果:李", Toast.LENGTH_LONG).show();
                }else if ("258".equals(prediction.name)) {
                    Toast.makeText(getApplicationContext(), "结果:X", Toast.LENGTH_LONG).show();
                }
            }else {
                Toast.makeText(getApplicationContext(), R.string.notfull, Toast.LENGTH_LONG).show();
            }
        }else{
            Toast.makeText(getApplicationContext(), R.string.notfind, Toast.LENGTH_LONG).show();
        }
    }
    @Override
    protected void onDestroy() {
        //杀掉进程
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());//关闭应用
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

转载于:https://www.cnblogs.com/lbangel/p/3463800.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来回答你的问题。以下是一个使用 Tkinter 和 FileDialog 实现打开和保存文件的示例程序: ```python import tkinter as tk from tkinter import filedialog class App(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.pack() self.create_widgets() def create_widgets(self): self.text = tk.Text(self) self.text.pack() self.open_button = tk.Button(self, text="打开文件", command=self.open_file) self.open_button.pack(side="left") self.save_button = tk.Button(self, text="保存文件", command=self.save_file) self.save_button.pack(side="left") def open_file(self): file_path = filedialog.askopenfilename() if file_path: with open(file_path, "r") as file: file_content = file.read() self.text.delete("1.0", tk.END) self.text.insert(tk.END, file_content) def save_file(self): file_path = filedialog.asksaveasfilename(defaultextension=".txt") if file_path: with open(file_path, "w") as file: file_content = self.text.get("1.0", tk.END) file.write(file_content) root = tk.Tk() app = App(master=root) app.mainloop() ``` 这个程序创建了一个简单的文本编辑器,包含打开文件和保存文件的按钮。当用户点击“打开文件”按钮时,程序会弹出一个文件对话框,用户可以选择要打开的文件。当用户点击“保存文件”按钮时,程序会弹出一个文件对话框,用户可以选择保存文件的路径和文件名。程序会将用户输入的内容保存到文件中。 希望这个示例对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值