android手势滑动——左右滑动效果实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
  * 手势监听
  *
  * @author lifengfeng
  *
  */
public class MainActivity extends Activity implements OnTouchListener,
         OnGestureListener {
     // 创建一个用于识别收拾的GestureDetector对象
     @SuppressWarnings ( "deprecation" )
     private GestureDetector detector = new GestureDetector( this );
     // 新建一个LinearLayout布局对象,这里是指主页面的布局
     private LinearLayout myLayout;
     // 限制最小移动像素
     private int FLING_MIN_DISTANCE = 110 ;
     // 定义的Toast提示框显示时间
     private int TIME_OUT = 1000 ;
     private static final String TAG = "Main" ;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         myLayout = (LinearLayout) findViewById(R.id.test_layout);
         // 为布局绑定监听
         myLayout.setOnTouchListener( this );
     }
 
     /**
      * 手势滑动时别调用
      */
     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
             float velocityY) {
         // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
         if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE) {
             // 向左滑动
             Toast.makeText( this , "向左滑动" , TIME_OUT).show();
         } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE) {
             // 向右滑动
             Toast.makeText( this , "向右滑动" , TIME_OUT).show();
         }
         return false ;
     }
 
     /**
      * 长按时被调用
      */
     @Override
     public void onLongPress(MotionEvent e) {
         Log.d(TAG, "触发长按回调" );
     }
 
     /**
      * 滚动时调用
      */
     @Override
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
             float distanceY) {
         return false ;
     }
 
     /**
      * 在按下动作时被调用
      */
     @Override
     public boolean onDown(MotionEvent e) {
         Log.d(TAG, "按下回调" );
         return false ;
     }
 
     /**
      * 按住时被调用
      */
     @Override
     public void onShowPress(MotionEvent e) {
         Log.d(TAG, "按住不松回调" );
     }
 
     /**
      * 抬起时被调用
      */
     @Override
     public boolean onSingleTapUp(MotionEvent e) {
         Log.d(TAG, "触发抬起回调" );
         return false ;
     }
 
     /**
      * 重写OnTouchListener的onTouch方法 此方法在触摸屏被触摸,即发生触摸事件(接触和抚摸两个事件)的时候被调用
      */
     @Override
     public boolean onTouch(View v, MotionEvent event) {
         detector.onTouchEvent(event);
         return true ;
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值