用Activity的onTouchEvent方法实现监听手指上下左右滑动
2014-04-21 13:37
676 查看
应用了Activity的ontouchEvent方法监听手指点击事件,手指滑动的时候会先按下,滑倒另一个地方再抬起,我们就可以根据按下的坐标和抬起的坐标算出用户是往哪一个方向滑动了。
package com.example.testtt; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.widget.Toast; public class MainActivity extends Activity { //手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2) float x1 = 0; float x2 = 0; float y1 = 0; float y2 = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onTouchEvent(MotionEvent event) { //继承了Activity的onTouchEvent方法,直接监听点击事件 if(event.getAction() == MotionEvent.ACTION_DOWN) { //当手指按下的时候 x1 = event.getX(); y1 = event.getY(); } if(event.getAction() == MotionEvent.ACTION_UP) { //当手指离开的时候 x2 = event.getX(); y2 = event.getY(); if(y1 - y2 > 50) { Toast.makeText(MainActivity.this, "向上滑", Toast.LENGTH_SHORT).show(); } else if(y2 - y1 > 50) { Toast.makeText(MainActivity.this, "向下滑", Toast.LENGTH_SHORT).show(); } else if(x1 - x2 > 50) { Toast.makeText(MainActivity.this, "向左滑", Toast.LENGTH_SHORT).show(); } else if(x2 - x1 > 50) { Toast.makeText(MainActivity.this, "向右滑", Toast.LENGTH_SHORT).show(); } } return super.onTouchEvent(event); } }
相关文章推荐
- 用Activity的onTouchEvent方法实现监听手指上下左右滑动
- 用Activity的onTouchEvent方法实现监听手指上下左右滑动
- 用Activity的onTouchEvent方法实现监听手指上下左右滑动
- Activity的onTouchEvent方法实现监听手指上下左右滑动
- 用Activity的onTouchEvent方法实现监听手指上下左右滑动
- 用Activity的onTouchEvent方法实现监听手指上下左右滑动
- 用Activity的onTouchEvent方法实现监听手指上下左右滑动
- android中使用Activity实现监听手指上下左右滑动
- Activity实现监听手指上下左右滑动
- Activity实现上下左右滑动监听onTouchEvent()方法
- jQuery简单实现上下,左右滑动的方法
- JavaScript实现监听移动端上下左右滑动事件
- RecycleView实现左右滑动监听和上下拖拽调换顺序
- js实现移动端手指左右上下滑动翻页效果
- 带webView的Activity中实现监听左右滑动
- activity 监听手势滑动 上下左右
- jquery实现上下左右滑动的方法
- Android 自定义View 实现手势监听,左右滑动,上下滑动
- jquery实现上下左右滑动的方法
- Appium 1.7 实现上下、左右滑动页面方法