zoukankan      html  css  js  c++  java
  • Android 手势水平监听判断

      1 package com.zihao.ui;
      2 
      3 import com.zihao.R;
      4 
      5 import android.os.Bundle;
      6 import android.app.Activity;
      7 import android.util.Log;
      8 import android.view.GestureDetector;
      9 import android.view.GestureDetector.OnGestureListener;
     10 import android.view.MotionEvent;
     11 import android.view.View;
     12 import android.view.View.OnTouchListener;
     13 import android.widget.LinearLayout;
     14 import android.widget.Toast;
     15 
     16 /**
     17  * 
     18     * @ClassName: MainActivity 
     19     * @Description: Android 手势水平监听判断 
     20     * @author zeze
     21     * @date 2015年11月3日 下午4:16:11 
     22     *
     23  */
     24 public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {
     25     // 创建一个用于识别收拾的GestureDetector对象
     26     @SuppressWarnings("deprecation")
     27     private GestureDetector detector = new GestureDetector(this);
     28     // 新建一个LinearLayout布局对象,这里是指主页面的布局
     29     private LinearLayout myLayout;
     30 
     31     private int FLING_MIN_DISTANCE = 120;// 移动最小距离
     32     private static final int FLING_MIN_VELOCITY = 200;// 移动最大速度
     33     // 定义的Toast提示框显示时间
     34     private int TIME_OUT = 1000;
     35     private static final String TAG = "Main";
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         setContentView(R.layout.activity_main);
     41         myLayout = (LinearLayout) findViewById(R.id.test_layout);
     42         // 为布局绑定监听
     43         myLayout.setOnTouchListener(this);
     44     }
     45 
     46     /**
     47      * 手势滑动时别调用
     48      */
     49     @Override
     50     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
     51         Log.d(TAG, "Y:"+(e1.getY() - e2.getY()));
     52         Log.d(TAG, "X:"+(e1.getX() - e2.getX()));
     53         // e1:第1个ACTION_DOWN MotionEvent
     54         // e2:最后一个ACTION_MOVE MotionEvent
     55         // velocityX:X轴上的移动速度(像素/秒)
     56         // velocityY:Y轴上的移动速度(像素/秒)
     57         // X轴的坐标位移大于FLING_MIN_DISTANCE,y轴的坐标位移小于30,且移动速度大于FLING_MIN_VELOCITY个像素/秒
     58         if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(e1.getY() - e2.getY()) < 100 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
     59             // 水平向左滑动
     60             Toast.makeText(this, "向左滑动", TIME_OUT).show();
     61         }
     62         else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(e1.getY() - e2.getY()) < 100 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
     63             // 水平向右滑动
     64             Toast.makeText(this, "向右滑动", TIME_OUT).show();
     65         }
     66         return false;
     67     }
     68 
     69     /**
     70      * 长按时被调用
     71      */
     72     @Override
     73     public void onLongPress(MotionEvent e) {
     74         Log.d(TAG, "触发长按回调");
     75         // Toast.makeText(this, "触发长按回调", TIME_OUT).show();
     76     }
     77 
     78     /**
     79      * 滚动时调用
     80      */
     81     @Override
     82     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
     83         return false;
     84     }
     85 
     86     /**
     87      * 在按下动作时被调用
     88      */
     89     @Override
     90     public boolean onDown(MotionEvent e) {
     91         Log.d(TAG, "按下回调");
     92         // Toast.makeText(this, "按下回调", TIME_OUT).show();
     93         return false;
     94     }
     95 
     96     /**
     97      * 按住时被调用
     98      */
     99     @Override
    100     public void onShowPress(MotionEvent e) {
    101         Log.d(TAG, "按住不松回调");
    102         // Toast.makeText(this, "按住不松回调", TIME_OUT).show();
    103     }
    104 
    105     /**
    106      * 抬起时被调用
    107      */
    108     @Override
    109     public boolean onSingleTapUp(MotionEvent e) {
    110         Log.d(TAG, "触发抬起回调");
    111         // Toast.makeText(this, "触发抬起回调", TIME_OUT).show();
    112         return false;
    113     }
    114 
    115     /**
    116      * 重写OnTouchListener的onTouch方法 此方法在触摸屏被触摸,即发生触摸事件(接触和抚摸两个事件)的时候被调用
    117      */
    118     @Override
    119     public boolean onTouch(View v, MotionEvent event) {
    120         detector.onTouchEvent(event);
    121         return true;
    122     }
    123 }
    View Code
      1 package com.zihao.ui;
      2 
      3 import com.zihao.R;
      4 
      5 import android.os.Bundle;
      6 import android.app.Activity;
      7 import android.util.Log;
      8 import android.view.GestureDetector;
      9 import android.view.GestureDetector.OnGestureListener;
     10 import android.view.MotionEvent;
     11 import android.view.View;
     12 import android.view.View.OnTouchListener;
     13 import android.widget.LinearLayout;
     14 import android.widget.Toast;
     15 
     16 /**
     17  * 
     18     * @ClassName: MainActivity 
     19     * @Description: Android 手势水平监听判断 
     20     * @author zeze
     21     * @date 2015年11月3日 下午4:16:11 
     22     *
     23  */
     24 public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {
     25     // 创建一个用于识别收拾的GestureDetector对象
     26     @SuppressWarnings("deprecation")
     27     private GestureDetector detector = new GestureDetector(this);
     28     // 新建一个LinearLayout布局对象,这里是指主页面的布局
     29     private LinearLayout myLayout;
     30 
     31     private int FLING_MIN_DISTANCE = 120;// 移动最小距离
     32     private static final int FLING_MIN_VELOCITY = 200;// 移动最大速度
     33     // 定义的Toast提示框显示时间
     34     private int TIME_OUT = 1000;
     35     private static final String TAG = "Main";
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         setContentView(R.layout.activity_main);
     41         myLayout = (LinearLayout) findViewById(R.id.test_layout);
     42         // 为布局绑定监听
     43         myLayout.setOnTouchListener(this);
     44     }
     45 
     46     /**
     47      * 手势滑动时别调用
     48      */
     49     @Override
     50     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
     51         Log.d(TAG, "Y:"+(e1.getY() - e2.getY()));
     52         Log.d(TAG, "X:"+(e1.getX() - e2.getX()));
     53         // e1:第1个ACTION_DOWN MotionEvent
     54         // e2:最后一个ACTION_MOVE MotionEvent
     55         // velocityX:X轴上的移动速度(像素/秒)
     56         // velocityY:Y轴上的移动速度(像素/秒)
     57         // X轴的坐标位移大于FLING_MIN_DISTANCE,y轴的坐标位移小于30,且移动速度大于FLING_MIN_VELOCITY个像素/秒
     58         if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(e1.getY() - e2.getY()) < 100 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
     59             // 水平向左滑动
     60             Toast.makeText(this, "向左滑动", TIME_OUT).show();
     61         }
     62         else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(e1.getY() - e2.getY()) < 100 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
     63             // 水平向右滑动
     64             Toast.makeText(this, "向右滑动", TIME_OUT).show();
     65         }
     66         return false;
     67     }
     68 
     69     /**
     70      * 长按时被调用
     71      */
     72     @Override
     73     public void onLongPress(MotionEvent e) {
     74         Log.d(TAG, "触发长按回调");
     75         // Toast.makeText(this, "触发长按回调", TIME_OUT).show();
     76     }
     77 
     78     /**
     79      * 滚动时调用
     80      */
     81     @Override
     82     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
     83         return false;
     84     }
     85 
     86     /**
     87      * 在按下动作时被调用
     88      */
     89     @Override
     90     public boolean onDown(MotionEvent e) {
     91         Log.d(TAG, "按下回调");
     92         // Toast.makeText(this, "按下回调", TIME_OUT).show();
     93         return false;
     94     }
     95 
     96     /**
     97      * 按住时被调用
     98      */
     99     @Override
    100     public void onShowPress(MotionEvent e) {
    101         Log.d(TAG, "按住不松回调");
    102         // Toast.makeText(this, "按住不松回调", TIME_OUT).show();
    103     }
    104 
    105     /**
    106      * 抬起时被调用
    107      */
    108     @Override
    109     public boolean onSingleTapUp(MotionEvent e) {
    110         Log.d(TAG, "触发抬起回调");
    111         // Toast.makeText(this, "触发抬起回调", TIME_OUT).show();
    112         return false;
    113     }
    114 
    115     /**
    116      * 重写OnTouchListener的onTouch方法 此方法在触摸屏被触摸,即发生触摸事件(接触和抚摸两个事件)的时候被调用
    117      */
    118     @Override
    119     public boolean onTouch(View v, MotionEvent event) {
    120         detector.onTouchEvent(event);
    121         return true;
    122     }
    123 }
    View Code
  • 相关阅读:
    Windowsforms 中对文件操作
    ADO.net增删改的使用
    ADO.net数据访问
    可空类型
    FineUI 页面跳转
    ASP.NET页面之间传递值的几种方式
    C# Find() 与 FindAll()方法的使用
    在Sql中将 varchar 值 '1,2,3,4,5,6' 转换成数据类型 int
    DataSet、DataTable、DataRow、DataColumn区别及使用实例
    C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串
  • 原文地址:https://www.cnblogs.com/zeze/p/4933529.html
Copyright © 2011-2022 走看看