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

Android的gesture的识别和自定义gesture

[日期:2011-12-11] 来源:Linux社区  作者:alex0203 [字体:大 中 小]
从手势库中识别出和在GestureOverlayView所画的手势;

mian.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:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="输入手势:" />  
  11.     <!-- 注意  android.gesture.要加,否则报错,估计是找不到包-->  
  12.     <android.gesture.GestureOverlayView  
  13.         android:id="@+id/gesture_overlay_view_test"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="0dip"  
  16.         android:layout_weight="1.0"  
  17.         android:gestureStrokeType="multiple" />  
  18.   
  19. </LinearLayout>  
这里要注意:android.gesture.GestureOverlayView这里的不能用GestureOverlayView

下面介绍如何向手势库中增加自定义的手势:

上代码:

GestureBuilderDemoActivity.java

  1. package com.potato;  
  2.   
  3. import java.io.File;  
  4. import java.util.Collections;  
  5. import java.util.Comparator;  
  6. import java.util.HashMap;  
  7. import java.util.Map;  
  8.   
  9. import android.app.Activity;  
  10. import android.content.Context;  
  11. import android.content.Intent;  
  12. import android.content.res.Resources;  
  13. import android.gesture.Gesture;  
  14. import android.gesture.GestureLibraries;  
  15. import android.gesture.GestureLibrary;  
  16. import android.graphics.Bitmap;  
  17. import android.graphics.drawable.BitmapDrawable;  
  18. import android.graphics.drawable.Drawable;  
  19. import android.os.Bundle;  
  20. import android.os.Environment;  
  21. import android.view.LayoutInflater;  
  22. import android.view.View;  
  23. import android.view.ViewGroup;  
  24. import android.widget.ArrayAdapter;  
  25. import android.widget.Button;  
  26. import android.widget.ListView;  
  27. import android.widget.TextView;  
  28.   
  29. public class GestureBuilderDemoActivity extends Activity {  
  30.   
  31.     private static final int STATUS_SUCCESS = 0;  
  32.     private static final int STATUS_CANCELLED = 1;  
  33.     private static final int STATUS_NO_STORAGE = 2;  
  34.     private static final int STATUS_NOT_LOADED = 3;  
  35.   
  36.     private static final int REQUEST_NEW_GESTURE = 1;  
  37.   
  38.     // 存放手势的文件   
  39.     private final File mStoreFile = new File(  
  40.             Environment.getExternalStorageDirectory(), "gestures");  
  41.   
  42.     private GesturesAdapter mAdapter;  
  43.     private static GestureLibrary mGestureLib;  
  44.   
  45.     private TextView mTvEmpty;  
  46.     Button mBtnAddGesture;  
  47.     Button mBtnIdentify;  
  48.   
  49.     Resources mRes;  
  50.     ListView mListViewGesture;  
  51.   
  52.     // 宽度、高度   
  53.     private int mThumbnailSize;  
  54.     // 密度   
  55.     private int mThumbnailInset;  
  56.     // 颜色   
  57.     private int mPathColor;  
  58.   
  59.     static GestureLibrary getStore() {  
  60.         return mGestureLib;  
  61.     }  
  62.   
  63.     /** 
  64.      * 手势排序 
  65.      */  
  66.     private final Comparator<NamedGesture> mSorter = new Comparator<NamedGesture>() {             
  67.         public int compare(NamedGesture object1, NamedGesture object2) {  
  68.             return object1.name.compareTo(object2.name);  
  69.         }  
  70.     };  
  71.   
  72.     /** Called when the activity is first created. */  
  73.     @Override  
  74.     public void onCreate(Bundle savedInstanceState) {  
  75.         super.onCreate(savedInstanceState);  
  76.         setContentView(R.layout.main);  
  77.   
  78.         mRes = getResources();  
  79.   
  80.         mBtnAddGesture = (Button) findViewById(R.id.addButton);  
  81.         mBtnIdentify = (Button) findViewById(R.id.reloadButton);  
  82.   
  83.         mPathColor = mRes.getColor(R.color.gesture_color);  
  84.         mThumbnailInset = (int) mRes  
  85.                 .getDimension(R.dimen.gesture_thumbnail_inset);  
  86.         mThumbnailSize = (int) mRes  
  87.                 .getDimension(R.dimen.gesture_thumbnail_size);  
  88.   
  89.         mListViewGesture = (ListView) findViewById(android.R.id.list);  
  90.         mAdapter = new GesturesAdapter(this);  
  91.         mListViewGesture.setAdapter(mAdapter);  
  92.   
  93.         if (mGestureLib == null) {  
  94.             mGestureLib = GestureLibraries.fromFile(mStoreFile);                // 注1   
  95.         }  
  96.         mTvEmpty = (TextView) findViewById(android.R.id.empty);  
  97.         loadGestures();                                                           
  98.   
  99.         if (mListViewGesture.getAdapter().isEmpty()) {  
  100.             mListViewGesture.setEmptyView(mTvEmpty);  
  101.             mBtnAddGesture.setEnabled(false);  
  102.         } else {  
  103.             mBtnAddGesture.setEnabled(true);  
  104.         }  
  105.     }  
  106.   
  107.     @Override  
  108.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  109.         super.onActivityResult(requestCode, resultCode, data);  
  110.   
  111.         if (resultCode == RESULT_OK) {  
  112.             switch (requestCode) {  
  113.             case REQUEST_NEW_GESTURE:  
  114.                 loadGestures();  
  115.                 break;  
  116.             }  
  117.         }  
  118.     }  
  119.   
  120.     /** 
  121.      * 识别手势 
  122.      */  
  123.     @SuppressWarnings({ "UnusedDeclaration" })  
  124.     public void identifyGestures(View v) {  
  125.         Intent intent = new Intent(this, GestureIdentifyDemoActivity.class);  
  126.         startActivity(intent);  
  127.     }  
  128.   
  129.     /** 
  130.      * 创建手势 
  131.      * @param v 
  132.      */  
  133.     @SuppressWarnings({ "UnusedDeclaration" })  
  134.     public void addGesture(View v) {  
  135.         Intent intent = new Intent(this, CreateGestureActivity.class);  
  136.         startActivityForResult(intent, REQUEST_NEW_GESTURE);  
  137.     }  
  138.   
  139.     /** 
  140.      * 加载手势 
  141.      * @return 
  142.      */  
  143.     private int loadGestures() {  
  144.         if (!Environment.MEDIA_MOUNTED.equals(Environment  
  145.                 .getExternalStorageState())) {  
  146.             return STATUS_NO_STORAGE;  
  147.         }  
  148.   
  149.         final GestureLibrary gestureLib = mGestureLib;  
  150.   
  151.         mAdapter.clear();  
  152.         if (gestureLib.load()) {                                        // 注2   
  153.             for (String name : gestureLib.getGestureEntries()) {  
  154.   
  155.                 for (Gesture gesture : gestureLib.getGestures(name)) {  
  156.                     final Bitmap bitmap = gesture.toBitmap(mThumbnailSize,          // 注3   
  157.                             mThumbnailSize, mThumbnailInset, mPathColor);  
  158.                     final NamedGesture namedGesture = new NamedGesture();  
  159.                     namedGesture.gesture = gesture;  
  160.                     namedGesture.name = name;  
  161.   
  162.                     mAdapter.addBitmap(namedGesture.gesture.getID(), bitmap);  
  163.                     mAdapter.add(namedGesture);  
  164.                 }  
  165.             }  
  166.             mAdapter.sort(mSorter);                         // 注4   
  167.             mAdapter.notifyDataSetChanged();  
  168.             return STATUS_SUCCESS;  
  169.         }  
  170.   
  171.         return STATUS_NOT_LOADED;  
  172.     }  
  173.   
  174.     static class NamedGesture {  
  175.         String name;  
  176.         Gesture gesture;  
  177.     }  
  178.   
  179.     private class GesturesAdapter extends ArrayAdapter<NamedGesture> {  
  180.         private final LayoutInflater mInflater;  
  181.         private final Map<Long, Drawable> mThumbnails = Collections  
  182.                 .synchronizedMap(new HashMap<Long, Drawable>());  
  183.   
  184.         public GesturesAdapter(Context context) {  
  185.             super(context, 0);  
  186.             mInflater = (LayoutInflater) context  
  187.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  188.         }  
  189.   
  190.         void addBitmap(Long id, Bitmap bitmap) {  
  191.             mThumbnails.put(id, new BitmapDrawable(bitmap));  
  192.         }  
  193.   
  194.         @Override  
  195.         public View getView(int position, View convertView, ViewGroup parent) {  
  196.             if (convertView == null) {  
  197.                 convertView = mInflater.inflate(R.layout.list_item_gesture,  
  198.                         parent, false);  
  199.             }  
  200.   
  201.             final NamedGesture gesture = getItem(position);  
  202.             final TextView label = (TextView) convertView;  
  203.   
  204.             label.setTag(gesture);  
  205.             label.setText(gesture.name);  
  206.             label.setCompoundDrawablesWithIntrinsicBounds(  
  207.                     mThumbnails.get(gesture.gesture.getID()), null, null, null);  
  208.   
  209.             return convertView;  
  210.         }  
  211.     };  
  212. }  
注1
  1. mGestureLib = GestureLibraries.fromFile(mStoreFile);  
从SD卡中加载已有的手势;

注2

  1. gestureLib.load()  
从手势库加载手势


注3

  1. final Bitmap bitmap = gesture.toBitmap(mThumbnailSize,          // 注3   
  2.                             mThumbnailSize, mThumbnailInset, mPathColor);  
把手势转换成bitmap

注4

排序

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

       

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