文章目录 [隐藏]
一。 extends Activity implements OnTouchListener, OnGestureListener
1.声明成员变量GestureDetector
1 2 3 4 |
/** 手势识别器 android.view.GestureDetector **/ private GestureDetector gd; |
2.为界面布局添加触摸事件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guide_for_security_activity1); // 创建GesTureDetector对象(创建时必须实现OnGestureListnener监听器实例)GestureDetector gd; gd = new GestureDetector((OnGestureListener)this); // 为当前Acitivity的布局页面添加setOnTouchListener事件 LinearLayout ll_gfsa1 = (LinearLayout) findViewById(R.id.ll_gfsa1); ll_gfsa1.setOnTouchListener(this); ll_gfsa1.setLongClickable(true); |
3.实现抽象类方法
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 |
// ------------用户按下触摸屏、快速移动后松开------------- // e1 手势起点的移动事件。得到手指点下的位置 // e2 当前手势点的移动事件 // velocityX 每秒x轴方向移动的像素 // velocityY 每秒y轴方向移动的像素 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { int FLING_MIN_DISTANCE/*最短移动距离*/ = 100, FLING_MIN_VELOCITY/*速度,每秒最小移动距离*/ = 200; // 手指从右向左滑动,打开右侧的界面 if(e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { Intent intent = new Intent(GuideForSecurityActivity1.this, GuideForSecurityActivity2.class); startActivity(intent); }/* else // 手指从屏幕左侧点下,向右滑动 if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) >FLING_MIN_VELOCITY) { //切换Activity Intent intent = new Intent(GuideForSecurityActivity1.this, SecurityActivity.class); startActivity(intent); }*/ return false; } // 注意:若不加setLongClickable(true)的话OnFling会失效,如果不写这句的话OnGestureListener的重写方法OnDown方法返回true也可以。 // 只有这样,view才能够处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN), // 我们同样可以通过layout定义中的android:longClickable来做到这一点。 // 将Acityvity的TouchEvent事件交给GestureDetector处理 @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub return gd.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } // 用户(轻触触摸屏后)松开 public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } // 用户按下触摸屏,并拖动 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } // 用户长按触摸屏 public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } |
二。直接使用GestureDetector进行事件监听
1.声明手势识别器
1 2 3 4 |
/** 手势识别器 **/ private android.view.GestureDetector gd; |
2.创建识别器
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 |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guide_for_security_activity2); // 创建GesTureDetector对象 gd = new GestureDetector(GuideForSecurityActivity2.this, new SimpleOnGestureListener() { // ------------用户按下触摸屏、快速移动后松开------------- // e1 手势起点的移动事件。得到手指点下的位置 // e2 当前手势点的移动事件 // velocityX 每秒x轴方向移动的像素 // velocityY 每秒y轴方向移动的像素 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { int FLING_MIN_DISTANCE/*最短移动距离*/ = 100, FLING_MIN_VELOCITY/*速度,每秒最小移动距离*/ = 200; // 手指从右向左滑动,打开右侧的界面 if(e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { Intent intent = new Intent(GuideForSecurityActivity2.this, GuideForSecurityActivity3.class); startActivity(intent); return true; } else // 手指从屏幕左侧点下,向右滑动 if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) >FLING_MIN_VELOCITY) { //切换Activity Intent intent = new Intent(GuideForSecurityActivity2.this, GuideForSecurityActivity1.class); startActivity(intent); return true; } return false; } }); |
3.实现Activity的onTouchEvent
1 2 3 4 5 6 7 |
/** 这里是Activity的Touch事件 **/ public boolean onTouchEvent(MotionEvent event) { gd.onTouchEvent(event); return super.onTouchEvent(event); } |
三。抽取功能然后继承
1.将功能抽取到父类
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 |
public abstract class GuideForSecurityActivityBase extends Activity { /** 手势识别器 **/ private android.view.GestureDetector gd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 创建GesTureDetector对象 gd = new GestureDetector(this, new SimpleOnGestureListener() { public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { int FLING_MIN_DISTANCE/* 最短移动距离 */= 100, FLING_MIN_VELOCITY/* 速度,每秒最小移动距离 */= 200; // 手指从右向左滑动,打开右侧的界面 if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { showNextActivity(); } else // 手指从屏幕左侧点下,向右滑动 if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { showPrevActivity(); } return false; } }); } private void showPrevActivity() { goPrevActivity(); } private void showNextActivity() { goNextActivity(); } /** 这里是Activity的Touch事件 **/ public boolean onTouchEvent(MotionEvent event) { gd.onTouchEvent(event); return super.onTouchEvent(event); } /** 定义2个抽象方法,让子类实现界面的跳转 **/ public abstract void goNextActivity(); /** 定义2个抽象方法,让子类实现界面的跳转 **/ public abstract void goPrevActivity(); } |
2.子类继承父类
1 2 3 |
public class GuideForSecurityActivity4 extends GuideForSecurityActivityBase { |
3.实现父类的抽象方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Override public void goNextActivity() { Intent intent = new Intent(GuideForSecurityActivity3.this, GuideForSecurityActivity4.class); startActivity(intent); // 下一个界面 overridePendingTransition(R.anim.anim_translate_activity_in, R.anim.anim_translate_activity_out); } @Override public void goPrevActivity() { Intent intent = new Intent(GuideForSecurityActivity3.this, GuideForSecurityActivity2.class); startActivity(intent); } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/676.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设