码迷,mamicode.com
首页 > 移动开发 > 详细

【Android】手势输入拨号器

时间:2015-07-06 14:23:23      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:android   手势   输入   拨号   adb   

主要是通过这个小app来说明怎么对安卓的app添加手势操作。

如下图,用户可以通过手势输入数字,最终,通过手势输入完号码之后,点击“拨号”按钮可以拨号。

技术分享


一、手势准备

1、为了实现这个app,我们先要准备0-9这10个数字的手势。首先,你必须先打开的你的安卓模拟器AVD,里面有一个gestures builder的工具,想打开普通app一样,打开它。

技术分享

2、这个系统工具的app在所有AVD里面都会存在的。普通的手机、平板是没有的,开发人员工具。点击右下角的Add gesture可以进入添加手势界面。

技术分享

3、在添加手势界面,输入手势名称,在手势名称下面的空白处,绘制手势,点击Done,此手势则会被自动保存。值得注意的是,这里的所有手势必须通过一笔来完成,市面上许多多笔识别的app,很多是通过,多个一笔画的手势所并接识别所完成的。

技术分享

4、可以看到,按Done之后,手势会自动存储到内存卡的根目录下的gestures文件夹。添加完的手势如果不满意,长按会弹出删除菜单。此时可以如2一样,继续点击左下角的Add gesture添加手势,如果添加完毕,直接按返回键退出即可。

技术分享

5、之后,在你Eclipse下的安卓工程下的res文件夹新建一个raw文件夹,通过DDMS工具,把AVD生成的手势文件gesture导入到raw文件夹。DDMS工具的使用在《【Android】把外部文件拷贝的AVD安卓模拟器上的sdcard上,并且在AVD中浏览sdcard的文件》(点击打开链接)已经说过了,这里不再赘述。理论上,AVD把生成的gesture文件放到内存卡的根目录上,在DDMS工具上的mnt\sdcard是可以见到的。如果部分Eclipse读不到,可以打开命令行,输入如下的命令:

adb remount
更新一下,则能够在mnt\media_rw读取到AVD内存卡的内容。

技术分享

6、把AVD内存卡的gestures拷贝到Eclipse工程目录的res\raw之后,刷新一下工程,检查一下gestures文件是否已经注册至gen\R.java,如果已注册,则如下图所示,这个gestures是有相应的机器码的。至此,手势准备完毕,可以正式开始编程。

技术分享


二、app代码的编写

1、首先res\values\strings.xml下的各个组件设置如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">手势输入拨号器</string>
    <string name="action_settings">Settings</string>
    <string name="button1">拨号!</string>
    <string name="textview1">请在如下进行手写输入:</string>

</resources>
2、之后,由于此app需要获取拨打电话的操作,所以要在AndroidManifest.xml中注册相应的权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.guestInput"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.CALL_PHONE" /> <!-- 获取拨打电话的权限 -->
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.guestInput.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
3、其次,修改MainActivity的布局文件res\layout\activity_main.xml如下,整个文件没什么好说的,垂直的线性布局,从上至下,分别是编辑框、按钮、一个标签文本。关键是放一个手势输入区域。

<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:id="@+id/edittext1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:textSize="24sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:textSize="24sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textview1"
        android:textSize="18sp" />

    <android.gesture.GestureOverlayView
        android:id="@+id/gestures1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>
4、最后修改MainActivity.java如下,这里分别通过接口的方式注册了手势事件与按钮被点击事件。这里与之前使用匿名内部类注册事件是一样的。具体的内容请参考其中的注释。Activity通过Intent拨打电话的内容,可以参考《【Android】使用Intent调用系统其它程序,使用onKeyDown对音量键的监听,长按事件》(点击打开链接)。

package com.guestInput;

import java.util.ArrayList;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
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.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;

public class MainActivity extends Activity implements
		OnGesturePerformedListener, OnClickListener {

	private GestureLibrary gestureLibrary;
	private GestureOverlayView gestureOverlayView;
	private EditText edittext1;
	private Button button1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 加载相应的xml设置
		setContentView(R.layout.activity_main);
		gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);// 加载手势文件
		if (!gestureLibrary.load()) {
			System.exit(0);// 如果加载失败便终结此程序。一般不会失败的。
		}
		// 获取要操作的组件
		edittext1 = (EditText) findViewById(R.id.edittext1);
		button1 = (Button) findViewById(R.id.button1);
		gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestures1);// 在视图中加载手势
		gestureOverlayView.addOnGesturePerformedListener(this);// 并且加载手势的监听器
		button1.setOnClickListener(this);// 为button1添加点击事件
	}

	// 手势事件
	@Override
	public void onGesturePerformed(GestureOverlayView arg0, Gesture gesture) {
		// TODO Auto-generated method stub
		ArrayList<Prediction> predictionList = gestureLibrary
				.recognize(gesture);
		int index = 0;
		double score = 0.0;
		for (int i = 0; i < predictionList.size(); i++) {// 获取最佳匹配结果
			Prediction prediction = predictionList.get(i);
			if (prediction.score > score) {// 求出最贴近的手势,相当于一个求最大值的过程
				index = i;// 记录最佳匹配的结果
				score = prediction.score;
			}
		}
		// 得到相应的手势之后,edittext1中的内容
		String text = edittext1.getText() + "";
		text += predictionList.get(index).name;
		edittext1.setText(text);
	}

	// 按钮被点击事件
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		String phoneNumber = edittext1.getText() + "";
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:" + phoneNumber));
		Toast.makeText(MainActivity.this, "开始拨打电话!", Toast.LENGTH_LONG).show();
		startActivity(intent);
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

【Android】手势输入拨号器

标签:android   手势   输入   拨号   adb   

原文地址:http://blog.csdn.net/yongh701/article/details/46772177

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!