当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

3.8 Gestures(手势)

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:26:06

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化SimpleOnGestureListener与GestureDetector对象 mgListener = new MyGestureListener(); mDetector = new GestureDetector(this, mgListener); } @Override public boolean onTouchEvent(MotionEvent event) { return mDetector.onTouchEvent(event); } //自定义一个GestureListener,这个是View类下的,别写错哦!!! private class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) { if(e1.getY() - e2.getY() > MIN_MOVE){ startActivity(new Intent(MainActivity.this, MainActivity.class)); Toast.makeText(MainActivity.this, "通过手势启动Activity", Toast.LENGTH_SHORT).show(); }else if(e1.getY() - e2.getY() < MIN_MOVE){ finish(); Toast.makeText(MainActivity.this,"通过手势关闭Activity",Toast.LENGTH_SHORT).show(); } return true; } }}结果分析:从上面的对比就可以知道,相比起SimpleOnGestureListener使用SimpleOnGestureListener显得更加的简单,想重写什么方法就重写什么方法,另外例子比较简单,大家可以自己试试其他玩法,比如通过手势缩放图片~4.手势添加与识别:除了上面讲解的手势检测外,Android还运行我们将手势进行添加,然后提供了相关的识别API;Android中使用GestureLibrary来代表手势库,提供了GestureLibraries工具类来创建手势库!四个加载手势库的静态方法:获得GestureLibraries对象后,就可以使用该对象提供的下述方法来做相应操作了:相关方法:public void addGesture (String entryName, Gesture gesture):添加一个名为entryName的手势public Set<String> getGestureEntries ():获得手势库中所有手势的名称public ArrayList<Gesture> getGestures (String entryName):获得entryName名称对应的全部手势public ArrayList<Prediction> recognize (Gesture gesture): 从当前手势库中识别与gesture匹配的全部手势public void removeEntry (String entryName):删除手势库中entryName名称对应的手势public void removeGesture (String entryName, Gesture gesture):删除手势库中entryName和gesture都匹配的手势public abstract boolean save ():想手势库中添加手势或从中删除手势后调用该方法保存手势库GestureOverlayView手势编辑组件:Android为GestureOverlayView提供了三种监听器接口,如下,一般常用的是:OnGesturePerformedListener;用于手势完成时提供响应!5.手势添加示例:PS:例子引用的是——李刚《Android疯狂讲义》的代码运行效果图:好吧,下面贴下实现代码:两个布局文件:activity_main.xml和dialog_save.xmlactivity_main.xml<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" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请在下方屏幕中绘制手势~" android:textSize="20sp"/> <!-- gestureStrokeType控制手势是否需要一笔完成,multiple表示允许多笔--> <android.gesture.GestureOverlayView android:id="@+id/gesture" android:layout_width="match_parent" android:layout_height="match_parent" android:gestureStrokeType="multiple" /></LinearLayout>dialog_save.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" android:text="请填写手势名称:"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit_name"/> </LinearLayout> <ImageView android:id="@+id/img_show" android:layout_width="128dp" android:layout_height="128dp" android:layout_marginTop="10dp"/></LinearLayout>MainActivity.java:public class MainActivity extends AppCompatActivity { private EditText editText; private GestureOverlayView gesture; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取手势编辑组件后,设置相关参数 gesture = (GestureOverlayView) findViewById(R.id.gesture); gesture.setGestureColor(Color.GREEN); gesture.setGestureStrokeWidth(5); gesture.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() { @Override public void onGesturePerformed(GestureOverlayView gestureOverlayView, final Gesture gesture) { View saveDialog = getLayoutInflater().infla

上一页  [1] [2] [3]  下一页


3.8 Gestures(手势)