Android手势操作
2013-07-18 18:51
204 查看
xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity" > <android.gesture.GestureOverlayView android:id="@+id/gestureOverlayView1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gestureStrokeType="multiple" > </android.gesture.GestureOverlayView> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:id="@+id/Button" android:layout_weight="0" android:text="识别"/> </LinearLayout> 注意: android_weight=0 表示是首先计算该控件的的高度或宽度,然后计算android_weight="1"控件的 高度,一般他是和 android:layout_height="0dp"结合使用的,否则是不会出来效果 public class MainActivity extends Activity { private GestureLibrary library; private Gesture gestures; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GestureOverlayView gesture=(GestureOverlayView) this.findViewById(R.id.gestureOverlayView1); library=GestureLibraries.fromRawResource(this, R.raw.gestures);//得到手势文件的类文件 library.load();//加载手势库 /** * 为手势添加事件监听 */ gesture.addOnGestureListener(new OnGestureListener(){ public void onGesture(GestureOverlayView overlay, MotionEvent event) { // TODO Auto-generated method stub } @Override public void onGestureCancelled(GestureOverlayView overlay,MotionEvent event) { // TODO Auto-generated method stub } @Override public void onGestureEnded(GestureOverlayView overlay,MotionEvent event) { gestures=overlay.getGesture();//得到一个手势 } @Override public void onGestureStarted(GestureOverlayView overlay,MotionEvent event) { // TODO Auto-generated method stub } }); this.findViewById(R.id.Button).setOnClickListener(new OnClickListener() { public void onClick(View v) { find(gestures); } }); } public void find(Gesture gesture) { ArrayList<Prediction> predictions=library.recognize(gesture);//得到手势库中的一个集合 if(predictions!=null) { Prediction prediction= predictions.get(0);//得到一个最匹配的一项,手势匹配默认会把最匹配的一项放在最前面 /** * 判断匹配的度数是否大于60%,prediction.score的值在大于0小于10的,如果是4的话就是匹配值是40%的意思 */ if(prediction.score>5) { if("close".equals(prediction.name)) { finish();//此方法只能那个关闭Activity,并不能杀死进程,所以要在OnDestory()方法中杀死进程 } else if("call".equals(prediction.name)) { Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:15824935047")); startActivity(intent); } } else { Toast.makeText(this, "匹配值太低", Toast.LENGTH_LONG).show(); } }else { Toast.makeText(this, "匹配不成功", Toast.LENGTH_LONG).show(); } } @Override protected void onDestroy() { Process.killProcess(Process.myPid());//杀死当前进程 super.onDestroy(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
相关文章推荐
- android 手势操作 滑动效果 触摸屏事件处理
- (转)【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- android手势操作滑动效果触摸屏事件处理
- Android 添加手势操作
- Android中的手势操作
- Android的手势操作(Gesture)
- Android游戏开发:实现手势操作切换图片的实例
- Android 触摸及手势操作GestureDetector
- 【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- Android onTouch事件与手势操作
- Android的手势操作识别
- android手势操作滑动效果触摸屏事件处理
- Android开源项目-Jamendo音乐播放器研究与优化-手势操作
- Android手势操作(二)
- Android手势操作
- Android手势操作编程
- android手势操作滑动效果触摸屏事件处理
- 自定义控件: android中手势操作图片的平移、缩放、旋转 并保存
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
- Android 触摸及手势操作GestureDetector