Android中怎么检测屏幕手势,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
layout/activity_gesture_detector.xml界面布局代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingTop="10dp"> <TextView android:id="@+id/tv_gesture" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="这里查看手势结果" android:textColor="@color/black" android:textSize="17sp" /></LinearLayout>
GestureDetectorActivity.java逻辑代码如下:
package com.fukaimei.gesturedetector;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.GestureDetector;import android.view.MotionEvent;import android.widget.TextView;import com.fukaimei.gesturedetector.util.DateUtil;public class GestureDetectorActivity extends AppCompatActivity { private TextView tv_gesture; private GestureDetector mGesture; private String desc = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gesture_detector); tv_gesture = (TextView) findViewById(R.id.tv_gesture); mGesture = new GestureDetector(this, new MyGestureListener()); } public boolean dispatchTouchEvent(MotionEvent event) { mGesture.onTouchEvent(event); return true; } final class MyGestureListener implements GestureDetector.OnGestureListener { @Override public final boolean onDown(MotionEvent event) {// desc = String.format("%s%s 您按下来了\n", desc, DateUtil.getNowTime());// tv_gesture.setText(desc); //onDown的返回值没有作用,不影响其它手势的处理 return true; } @Override public final boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { float offsetX = e1.getX() - e2.getX(); float offsetY = e1.getY() - e2.getY(); if (Math.abs(offsetX) > Math.abs(offsetY)) { if (offsetX > 0) { desc = String.format("%s%s 您向左滑动了一下\n", desc, DateUtil.getNowTime()); } else { desc = String.format("%s%s 您向右滑动了一下\n", desc, DateUtil.getNowTime()); } } else { if (offsetY > 0) { desc = String.format("%s%s 您向上滑动了一下\n", desc, DateUtil.getNowTime()); } else { desc = String.format("%s%s 您向下滑动了一下\n", desc, DateUtil.getNowTime()); } } tv_gesture.setText(desc); return true; } @Override public final void onLongPress(MotionEvent event) { desc = String.format("%s%s 您长按了一下下\n", desc, DateUtil.getNowTime()); tv_gesture.setText(desc); } @Override public final boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public final void onShowPress(MotionEvent event) { } @Override public boolean onSingleTapUp(MotionEvent event) { desc = String.format("%s%s 您轻轻点了一下\n", desc, DateUtil.getNowTime()); tv_gesture.setText(desc); //返回true表示我已经处理了,别处不要再处理这个手势 return true; } }}
Demo程序运行效果界面截图如下:
关于Android中怎么检测屏幕手势问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网行业资讯频道了解更多相关知识。