手势翻页代码

package com.innofidei.myprotector.helper;


import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;


public class TestFlip extends Activity implements OnGestureListener {


private ViewFlipper flipper;


private GestureDetector detector;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


detector = new GestureDetector(this);
flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);


flipper.addView(addTextView("step 1"));
flipper.addView(addTextView("step 2"));
flipper.addView(addTextView("step 3"));
flipper.addView(addTextView("step 4"));
flipper.addView(addTextView("step 5"));
}


private View addTextView(String text) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setGravity(1);
return tv;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
}


@Override
public boolean onDown(MotionEvent e) {
return false;
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > 120) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
this.flipper.showNext();
return true;
} else if (e1.getX() - e2.getX() < -120) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
this.flipper.showPrevious();
return true;
}
return false;
}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}


@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}


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


}


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


}
}

你可能感兴趣的:(手势翻页代码)