手势(Gesture)

xiaoxiao2021-02-28  320

一、Android提供的两种手势:①Android提供了手势检测,并为手势提供了相应的监听器       ②Android允许开发者添加手势,并提供了相应的API识别用户手势 二、手势检测:手势检测器类:GestureDetector  监听器:OnGestureListener,负责对用户的手势行为提供响应  时间处理方法:boolean OnDraw(MotionEvent e):当触摸事件按下时触发该方法   boolean OnFing(MotionEvent e1,MotionEvent e2,float velocity X,float velocity Y):当用户在触摸屏上“拖过”时触发该方法。其中 velocity X,float velocity Y代表“拖过”动作在横向,纵向上的速度 abstract void onLongPress(MotionEvent e):当用户在屏幕上长按时触发该方法 onScroll(MotionEvent e,MotionEvent e2,float distanceX,float distanceY):当用户在屏幕上“滚动”时触发该方法 void onShowPress(MotionEvent e):当用户在触摸屏上按下,而还未移动和松开时触发该方法 Android收拾检测步骤:第一步:创建一个GestureDetector对象。创建该对象时必须实现一个GestureDetector.OnGestureListener监听器实例:例如:GestureDetector detector=new GestureDetector(this,this)     第二步:为应用程序的Activity的TouchEvent事件绑定监听器,在事件处理中指定Activity上的TouchEvent事件交给GestureDetector处理。例如:detector.OnTouchEvent(event) 例子:①演示事件处理的方法       ②通过手势实现翻页效果: ViewFlipper组件,该组件可使用动画控制多个组件之间的切换效果 flipper.setInAnimation()设置组件进入的动画 flipper.setOutAnimation()设置组件出去的动画 flipper.showPrevious()显示上一个视图 flipper.showNext()显示下一个视图 三、增加手势 介绍:Android除了提供手势检测之外,还允许应用程序把用户手势(多个持续的触摸事件在屏幕上形成特定的形状)添加到指定的文件中,以备以后使用。如果程序需要,当用户下次再画出该手势时,系统将会识别该手势 手势库:GestureLibraryvoid 1.void addGesture(String entryName,Gesture gesture):添加一个名为entryName的手势 2.Set<String> getGestureEntries():获取该手势库中所有的手势的名称 3.ArrayList<Gesture> getGestures(String entryName):获取entryName名称对应的所有的手势 4.ArrayList<Prediction> recognize(Gesture gesture):从当前手势库中识别与gesture匹配的全部手势

5.void removeEntry(String entryName):删除手势库中entryName对应的手势

代码示例:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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"     tools:context="com.zking.g150715_android21_gesture.MainActivity">     <ViewFlipper         android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/vf_main_image"         >     </ViewFlipper> </LinearLayout>

----MainAcitvity.java

package com.zking.g150715_android21_gesture; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.widget.ImageView; import android.widget.Toast; import android.widget.ViewFlipper; public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener{     private ViewFlipper vf_main_image;     private int images[]={R.drawable.s1,R.drawable.s2,R.drawable.s3};     private GestureDetector gesture;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         vf_main_image = (ViewFlipper) findViewById(R.id.vf_main_image);         //给ViewFilpper设置适配器(不能)         for (int i = 0; i < images.length; i++) {             ImageView iv=new ImageView(this);             iv.setImageResource(images[i]);             vf_main_image.addView(iv);         }         //实例化一个手势检测器的类         gesture = new GestureDetector(this,this);     }     @Override     public boolean onTouchEvent(MotionEvent event) {         return gesture.onTouchEvent(event);     }     @Override     public boolean onDown(MotionEvent motionEvent) {         Log.i("test","按下");         return false;     }     @Override     public void onShowPress(MotionEvent motionEvent) {         Log.i("test","按下,按下但是还没有开始移动");     }     @Override     public boolean onSingleTapUp(MotionEvent motionEvent) {         Log.i("test","按一下");         return false;     }     @Override     public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {         Log.i("test","滑动");         return false;     }     @Override     public void onLongPress(MotionEvent motionEvent) {         Log.i("test","长按");     }     @Override     public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {         if(e1.getX()-e2.getX()>=200){             Toast.makeText(MainActivity.this, "←←←←←", Toast.LENGTH_SHORT).show();             vf_main_image.showNext();//下一张             //左出左进             vf_main_image.setOutAnimation(this,R.anim.left_out);             vf_main_image.setInAnimation(this,R.anim.left_in);         }else if(e2.getX()-e1.getX()>=200){             Toast.makeText(MainActivity.this, "→→→→→", Toast.LENGTH_SHORT).show();             vf_main_image.showPrevious();//上一张             //右出右进             vf_main_image.setOutAnimation(this,R.anim.right_out);             vf_main_image.setInAnimation(this,R.anim.right_in);         }         return false;     } }

转载请注明原文地址: https://www.6miu.com/read-47539.html

最新回复(0)