先要了解ViewFlipper,详细见:
http://gundumw100.iteye.com/admin/blogs/896840
OnGestureListener和OnDoubleTapListener接口定义:
实例:
http://gundumw100.iteye.com/admin/blogs/896840
OnGestureListener和OnDoubleTapListener接口定义:
- public interface OnGestureListener {
- // Touch down时触发, e为down时的MotionEvent
- boolean onDown(MotionEvent e);
- // 在Touch down之后一定时间(115ms)触发,e为down时的MotionEvent
- void onShowPress(MotionEvent e);
- // Touch up时触发,e为up时的MotionEvent
- boolean onSingleTapUp(MotionEvent e);
- // 滑动时触发,e1为down时的MotionEvent,e2为move时的MotionEvent
- boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
- // 在Touch down之后一定时间(500ms)触发,e为down时的MotionEvent
- void onLongPress(MotionEvent e);
- // 滑动一段距离,up时触发,e1为down时的MotionEvent,e2为up时的MotionEvent
- boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
- }
- public interface OnDoubleTapListener {
- // 完成一次单击,并确定没有二击事件后触发(300ms),e为down时的MotionEvent
- boolean onSingleTapConfirmed(MotionEvent e);
- // 第二次单击down时触发,e为第一次down时的MotionEvent
- boolean onDoubleTap(MotionEvent e);
- // 第二次单击down,move和up时都触发,e为不同时机下的MotionEvent
- boolean onDoubleTapEvent(MotionEvent e);
- }
实例:
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ViewFlipper;
- public class FlingSlideActivity extends Activity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{
- private ViewFlipper mViewFlipper;
- private GestureDetector mGestureDetector;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mViewFlipper = (ViewFlipper) findViewById(R.id.flipper);
- Button button1 = (Button) findViewById(R.id.Button1);
- button1.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- mViewFlipper.showNext();
- }
- });
- Button button2 = (Button) findViewById(R.id.Button2);
- button2.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- mViewFlipper.showNext();
- }
- });
- Button button3 = (Button) findViewById(R.id.Button3);
- button3.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- mViewFlipper.showNext();
- }
- });
- mGestureDetector = new GestureDetector(this);
- }
- //别忘了覆盖onTouchEvent方法
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- return mGestureDetector.onTouchEvent(event);
- }
- //以下是OnGestureListener需要实现的方法
- public boolean onDown(MotionEvent e) {
- // TODO Auto-generated method stub
- return false;
- }
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- // TODO Auto-generated method stub
- if(e1.getX() > e2.getX()) {//向左滑动
- mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);
- mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);
- mViewFlipper.showNext();
- }else if(e1.getX() < e2.getX()) {//向右滑动
- mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in);
- mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out);
- mViewFlipper.showPrevious();
- }else {
- return false;
- }
- return true;
- }
- public void onLongPress(MotionEvent e) {
- // TODO Auto-generated method stub
- }
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
- float distanceY) {
- // TODO Auto-generated method stub
- return false;
- }
- public void onShowPress(MotionEvent e) {
- // TODO Auto-generated method stub
- }
- public boolean onSingleTapUp(MotionEvent e) {
- // TODO Auto-generated method stub
- return false;
- }
- //以下是OnDoubleTapListener需要实现的方法
- public boolean onDoubleTap(MotionEvent e) {
- // TODO Auto-generated method stub
- mViewFlipper.startFlipping(); //双击自动切换界面
- return true;
- }
- public boolean onDoubleTapEvent(MotionEvent e) {
- // TODO Auto-generated method stub
- return false;
- }
- public boolean onSingleTapConfirmed(MotionEvent e) {
- // TODO Auto-generated method stub
- if(mViewFlipper.isFlipping()){ //单击结束自动切换
- mViewFlipper.stopFlipping();
- }
- return false;
- }