分类: 嵌入式
2011-10-17 22:50:52
1、利用ViewFlipper装载多屏,利用GestureDetector监听划屏动作
2、main.xml
3、实现ViewFlipper和GestureDetector对象
private ViewFlipper flipper;//ViewFlipper实例
private GestureDetector detector;//触摸监听实例
4、在onCreate方法中初始化ViewFlipper
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this);// 初始化触摸探测
flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);// 获得ViewFlipper实例
flipper.addView(addTextView("step 1"));// 将View添加到flipper队列中
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.setBackgroundColor(2222);
tv.setGravity(1);
return tv;
}
5、重写onTouchEvent方法,实现触摸方法监听
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
}
6、在onFling方法中实现简单的划屏事件监听:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
this.flipper.showNext();//显示flipper中的下一个view
return true;
}
7、增强划动的动画效果
(1)在res下增加anim文件夹
(2)增加4个xml文件
left_in.xml:
android:duration="500" />
left_out.xml:
android:duration="500" />
right_in.xml:
android:duration="500" />
right_out.xml:
android:duration="500" />
6、在onFling方法中实现有动画效果的划屏事件监听:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > 120) {// 如果是从右向左滑动
// 注册flipper的进出效果
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.left_out));
this.flipper.showNext();
return true;
} else if (e1.getX() - e2.getX() < -120) {// 如果是从左向右滑动
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.right_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.right_out));
this.flipper.showPrevious();
return true;
}
return true;
}