如何通过GestureDetector实现屏幕事件监听(滑动切换Layout) (转)
内容如题:
1.SlideExample.java文件
package com.example; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.ViewGroup; public class SlideExample extends Activity { public ViewGroup container1, container2; private GestureDetector gestureDetector; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 监听屏幕动作事件 SampleGuest gestureListener = new SampleGuest(this); gestureDetector = new GestureDetector(gestureListener); container1 = (ViewGroup) findViewById(R.id.container1); container2 = (ViewGroup) findViewById(R.id.container2); } // called automatically, any screen action will Triggered it public boolean onTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) return true; else return false; } }
2.SampleGuest.java文件
package com.example; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.GestureDetector.OnGestureListener; import android.view.animation.AnimationUtils; public class SampleGuest implements OnGestureListener { SlideExample se; private static final int SWIPE_MAX_OFF_PATH = 100; private static final int SWIPE_MIN_DISTANCE = 100; private static final int SWIPE_THRESHOLD_VELOCITY = 100; public SampleGuest(SlideExample se) { this.se = se; } // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发 public boolean onDown(MotionEvent e) { Log.d("TAG", "[onDown]"); return true; } // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, // 多个ACTION_MOVE, 1个ACTION_UP触发 // e1:第1个ACTION_DOWN MotionEvent // e2:最后一个ACTION_MOVE MotionEvent // velocityX:X轴上的移动速度,像素/秒 // velocityY:Y轴上的移动速度,像素/秒 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; if ((e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { se.container1.setAnimation(AnimationUtils.loadAnimation(se, R.anim.push_left_out)); se.container2.setVisibility(View.VISIBLE); se.container2.setAnimation(AnimationUtils.loadAnimation(se, R.anim.push_right_in)); se.container1.setVisibility(View.GONE); } else if ((e2.getX() - e1.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { se.container1.setVisibility(View.VISIBLE); se.container1.setAnimation(AnimationUtils.loadAnimation(se, R.anim.push_left_in)); se.container2.setAnimation(AnimationUtils.loadAnimation(se, R.anim.push_right_out)); se.container2.setVisibility(View.GONE); } return true; } // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发 public void onLongPress(MotionEvent e) { Log.d("TAG", "[onLongPress]"); } // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.d("TAG", "[onScroll]"); return true; } // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发 // 注意和onDown()的区别,强调的是没有松开或者拖动的状态 public void onShowPress(MotionEvent e) { Log.d("TAG", "[onShowPress]"); } // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发 public boolean onSingleTapUp(MotionEvent e) { Log.d("TAG", "[onSingleTapUp]"); return true; } }
3.main.xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/container"> <RelativeLayout android:layout_width="fill_parent" android:id="@+id/container1" android:layout_height="fill_parent" android:visibility="visible"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/one" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:background="@drawable/gray" android:id="@+id/container2" android:visibility="gone" android:layout_height="fill_parent"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/three" /> </RelativeLayout> </RelativeLayout>
4.程序所用到的anim中的几个xml文件内容
push_left_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="1000"/> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="100" /> </set>
push_left_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="1000" /> <alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" /> </set>
push_right_in.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromXDelta="100%p"
- android:toXDelta="0"
- android:duration="1000"/>
- <alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" />
- </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="1000"/> <alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" /> </set>
push_right_out.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromXDelta="0"
- android:toXDelta="100%p"
- android:duration="1000"/>
- <alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" />
- </set>
posted on 2012-03-14 16:52 fanhongyue 阅读(475) 评论(0) 编辑 收藏 举报