你好,游客 登录 注册 搜索
背景:
阅读新闻

Android的gesture的识别和自定义gesture

[日期:2011-12-11] 来源:Linux社区  作者:alex0203 [字体:大 中 小]

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#009966"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ListView  
  9.         android:id="@android:id/list"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="0dip"  
  12.         android:layout_weight="1.0" />  
  13.   
  14.     <LinearLayout  
  15.         style="@android:style/ButtonBar"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:orientation="horizontal">  
  19.   
  20.         <Button  
  21.             android:id="@+id/addButton"  
  22.             android:layout_width="0dip"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_weight="1"  
  25.             android:enabled="false"  
  26.             android:onClick="addGesture"  
  27.             android:text="button_add" />  
  28.   
  29.         <Button  
  30.             android:id="@+id/reloadButton"  
  31.             android:layout_width="0dip"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_weight="1"  
  34.             android:onClick="identifyGestures"  
  35.             android:text="button_identify" />  
  36.   
  37.     </LinearLayout>  
  38.   
  39. </LinearLayout>  

创建手势

CreateGestureActivity.java

  1. package com.potato;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Environment;  
  6. import android.view.View;  
  7. import android.view.MotionEvent;  
  8. import android.gesture.GestureOverlayView;  
  9. import android.gesture.Gesture;  
  10. import android.gesture.GestureLibrary;  
  11. import android.widget.TextView;  
  12. import android.widget.Toast;  
  13.   
  14. import java.io.File;  
  15.   
  16. public class CreateGestureActivity extends Activity {  
  17.     private static final float LENGTH_THRESHOLD = 120.0f;  
  18.   
  19.     private Gesture mGesture;  
  20.     private View mDoneButton;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.   
  26.         setContentView(R.layout.create_gesture);  
  27.   
  28.         mDoneButton = findViewById(R.id.done);  
  29.   
  30.         GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);  
  31.         overlay.addOnGestureListener(new GesturesProcessor());  
  32.     }  
  33.   
  34.     @Override  
  35.     protected void onSaveInstanceState(Bundle outState) {  
  36.         super.onSaveInstanceState(outState);  
  37.   
  38.         if (mGesture != null) {  
  39.             outState.putParcelable("gesture", mGesture);  
  40.         }  
  41.     }  
  42.   
  43.     @Override  
  44.     protected void onRestoreInstanceState(Bundle savedInstanceState) {  
  45.         super.onRestoreInstanceState(savedInstanceState);  
  46.   
  47.         mGesture = savedInstanceState.getParcelable("gesture");  
  48.         if (mGesture != null) {  
  49.             final GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);  
  50.             overlay.post(new Runnable() {  
  51.                 public void run() {  
  52.                     overlay.setGesture(mGesture);  
  53.                 }  
  54.             });  
  55.   
  56.             mDoneButton.setEnabled(true);  
  57.         }  
  58.     }  
  59.   
  60.     /** 
  61.      * 增加手势的按钮 
  62.      * @param v 
  63.      */  
  64.     public void addGesture(View v) {  
  65.         if (mGesture != null) {  
  66.             final TextView input = (TextView) findViewById(R.id.gesture_name);  
  67.             final CharSequence name = input.getText();  
  68.             if (name.length() == 0) {  
  69.                 input.setError("输入gesture的名字!");  
  70.                 return;  
  71.             }  
  72.               
  73.             // 把手势增加到手势库   
  74.             final GestureLibrary store = GestureBuilderDemoActivity.getStore();         // 注1   
  75.             store.addGesture(name.toString(), mGesture);  
  76.             store.save();  
  77.   
  78.             setResult(RESULT_OK);  
  79.   
  80.             final String path = new File(  
  81.                     Environment.getExternalStorageDirectory(), "gestures")  
  82.                     .getAbsolutePath();  
  83.             Toast.makeText(this, "保存成功" + path, Toast.LENGTH_LONG).show();  
  84.         } else {  
  85.             setResult(RESULT_CANCELED);  
  86.         }  
  87.   
  88.         finish();  
  89.   
  90.     }  
  91.   
  92.     /** 
  93.      * 取消手势 
  94.      * @param v 
  95.      */  
  96.     public void cancelGesture(View v) {  
  97.         setResult(RESULT_CANCELED);  
  98.         finish();  
  99.     }  
  100.   
  101.     /** 
  102.      * 手势监听着 
  103.      * @author Administrator 
  104.      * 
  105.      */  
  106.     private class GesturesProcessor implements  
  107.             GestureOverlayView.OnGestureListener {  
  108.         public void onGestureStarted(GestureOverlayView overlay,  
  109.                 MotionEvent event) {  
  110.             mDoneButton.setEnabled(false);  
  111.             mGesture = null;  
  112.         }  
  113.   
  114.         public void onGesture(GestureOverlayView overlay, MotionEvent event) {  
  115.         }  
  116.   
  117.         public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {  
  118.             // 获取在GestureOverlayView手势   
  119.             mGesture = overlay.getGesture();                                        // 注2   
  120.             if (mGesture.getLength() < LENGTH_THRESHOLD) {  
  121.                 overlay.clear(false);  
  122.             }  
  123.             mDoneButton.setEnabled(true);  
  124.         }  
  125.   
  126.         public void onGestureCancelled(GestureOverlayView overlay,  
  127.                 MotionEvent event) {  
  128.         }  
  129.     }  
  130. }  
注1
  1. final GestureLibrary store = GestureBuilderDemoActivity.getStore();         // 注1   
  2.             store.addGesture(name.toString(), mGesture);  
  3.             store.save();  
手势增加到手势库

注2

获取在GestureOverlayView手势

linux
相关资讯       Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款