实现左右滑动切换Activity

1.封装OnGestureListener
(PS:为了代码的简洁性,将OnGestureListener封装成了一个类,只要将其作为一个成员变量,并仅仅实现onFling方法即可)

/**
 * 必须实现 onFling
 * 
 * @author Chuanhang.Gu
 * 
 */
public class OnGestureListener implements
        android.view.GestureDetector.OnGestureListener {
    final int FLING_MIN_DISTANCE = 50;
    final float FLING_MIN_VELOCITY = 50;

    @Override
    public boolean onDown(MotionEvent e) {
        // 修改为return true, 否者onFling不调用
        // 否则:需要在activity中添加setOnLongclick(true);
        // PS:鄙人理解:存在事件拦截冲突
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

}

2.在activity中实现OnTouchListener和实例化GestureDetector

public class MiddleActivity extends Activity implements OnTouchListener {
    GestureDetector gd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        textView.setText("Middle");
        textView.setBackgroundColor(getResources().getColor(R.color.green));
        setContentView(textView);
        textView.setOnTouchListener(this);
        gd = new GestureDetector(this, mFlingListener);
    }

    OnGestureListener mFlingListener = new com.example.viewpagerdemo1.OnGestureListener() {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
                    && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                Intent intent = new Intent(MiddleActivity.this,
                        LeftActivity.class);
                startActivity(intent);
                Toast.makeText(MiddleActivity.this, "向左手势", Toast.LENGTH_SHORT)
                        .show();
                overridePendingTransition(R.anim.in_from_right,
                        R.anim.out_to_left);

            } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
                    && Math.abs(velocityX) > FLING_MIN_VELOCITY) {

                // 切换Activity
                Intent intent = new Intent(MiddleActivity.this,
                        RightActivity.class);
                startActivity(intent);
                Toast.makeText(MiddleActivity.this, "向右手势", Toast.LENGTH_SHORT)
                        .show();
                overridePendingTransition(R.anim.in_from_left,
                        R.anim.out_to_right);

            }

            return false;
        }

    };

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.performClick();
        return gd.onTouchEvent(event);
    }
}

上面的原理还是比较简单的:给当前页面添加OnTouchListener,在该页面所有的touch事件都会调用onTouch方法,将其移交给GestureDetector来处理。在GestureDetector中添加了一个监听进行回调,移交OnGestureListener来处理。
3.添加activity切换的动画:
在调用startActivity之后,调用onPendingIntent ,启用activity切换动画。
比如in_from_left.xml(从屏幕左侧进入动画)

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >
    <translate
        android:duration="500"
        android:fromXDelta="-100%p"
        android:toXDelta="0%p" />
set>

4.小窍门,为了保持activity堆栈的简单性,可以将MiddleActivity的launchMode设置为singleTask
5.碰到的问题:当页面上存在可点击的东西的时候,在可点击的上面(比如Button上),滑动切换activity无效。
6.另附Demo,供大家参考
http://download.csdn.net/detail/guchuanhang/9053801

你可能感兴趣的:(特效)