• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
尚善若水
博客园    首页    新随笔    联系   管理    订阅  订阅

用ViewFlipper和GestureDetector实现手势翻页的效果

  首先在布局文件中要有个ViewFlipper,这个可以用来加载View。可以在加载的View中做动画,但只能显示一个View。具体的API中是这样说的:

    Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval.

  所以我在布局文件中放一个ViewFlipper就可以了,然后在向里面加入View:

1 <?xml version="1.0" encoding="utf-8"?>
2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7  <ViewFlipper
8 android:id="@+id/myviewflipper"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11 />
12  </LinearLayout>
13  

 

  在代码中加入5个View,TextView是这样的:

 

1 private TextView addTextView(String str) {
2 TextView tv = new TextView(this);
3 tv.setText(str);
4 tv.setGravity(1);
5 return tv;
6 }

 

1 vf.addView(addTextView("step1"));
2 vf.addView(addTextView("step2"));
3 vf.addView(addTextView("step3"));
4 vf.addView(addTextView("step4"));
5 vf.addView(addTextView("step5"));

继承OnGestureListener,同时重写一个onTouchEvent方法:

public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return this.gd.onTouchEvent(event);
}

 

  然后在onFling方法里面做操作,让ViewFlipper显示上一个View还是显示下一个View:

1 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
2 float velocityY) {
3 // TODO Auto-generated method stub
4 if((e1.getX() - e2.getX()) > 100) {
5 this.vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
6 this.vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
7 this.vf.showPrevious();
8 return true;
9 } else if((e1.getX() -e2.getX()) < -100) {
10 this.vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
11 this.vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
12 this.vf.showNext();
13 return true;
14 }
15 return false;
16 }

 

  同时在里面加入了动画,分为从左边进入,从左边出去,从右边进入,从右边出去:

  animation_left_in:

1 <?xml version="1.0" encoding="utf-8"?>
2  <set xmlns:android="http://schemas.android.com/apk/res/android">
3 <translate
4 android:fromXDelta="-100%p"
5 android:toXDelta="0%p"
6 android:duration="500"/>
7 <alpha
8 android:fromAlpha="0.1"
9 android:toAlpha="1.0"
10 android:duration="500"/>
11  </set>
12  

 

  animation_left_out:

 

1 <?xml version="1.0" encoding="utf-8"?>
2  <set xmlns:android="http://schemas.android.com/apk/res/android">
3 <translate
4 android:fromXDelta="0%p"
5 android:toXDelta="-100%p"
6 android:duration="500"/>
7 <alpha
8 android:fromAlpha="1.0"
9 android:toAlpha="0.1"
10 android:duration="500"/>
11  </set>
12  

 

  animation_right_in:

1 <?xml version="1.0" encoding="utf-8"?>
2  <set xmlns:android="http://schemas.android.com/apk/res/android">
3 <translate
4 android:fromXDelta="100%p"
5 android:toXDelta="0%p"
6 android:duration="500"/>
7 <alpha
8 android:fromAlpha="0.1"
9 android:toAlpha="1.0"
10 android:duration="500"/>
11  </set>
12

 

  animation_right_out:

 

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3 <translate
4 android:fromXDelta="0%p"
5 android:toXDelta="100%p"
6 android:duration="500"/>
7 <alpha
8 android:fromAlpha="1.0"
9 android:toAlpha="0.1"
10 android:duration="500"/>
11 </set>
12

 

  Activity:

 

1 package com.android.test;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.view.GestureDetector;
6 import android.view.GestureDetector.OnGestureListener;
7 import android.view.MotionEvent;
8 import android.view.animation.AnimationUtils;
9 import android.widget.TextView;
10 import android.widget.ViewFlipper;
11
12 public class TestViewFlipActivity extends Activity implements OnGestureListener {
13
14 private GestureDetector gd;
15 private ViewFlipper vf;
16
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.main);
21 gd = new GestureDetector(this);
22 vf = (ViewFlipper) findViewById(R.id.myviewflipper);
23 vf.addView(addTextView("step1"));
24 vf.addView(addTextView("step2"));
25 vf.addView(addTextView("step3"));
26 vf.addView(addTextView("step4"));
27 vf.addView(addTextView("step5"));
28 }
29
30 private TextView addTextView(String str) {
31 TextView tv = new TextView(this);
32 tv.setText(str);
33 tv.setGravity(1);
34 return tv;
35 }
36
37 @Override
38 public boolean onTouchEvent(MotionEvent event) {
39 // TODO Auto-generated method stub
40 return this.gd.onTouchEvent(event);
41 }
42
43 @Override
44 public boolean onDown(MotionEvent arg0) {
45 // TODO Auto-generated method stub
46 return false;
47 }
48
49 @Override
50 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
51 float velocityY) {
52 // TODO Auto-generated method stub
53 if((e1.getX() - e2.getX()) > 100) {
54 this.vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
55 this.vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
56 this.vf.showPrevious();
57 return true;
58 } else if((e1.getX() -e2.getX()) < -100) {
59 this.vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
60 this.vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
61 this.vf.showNext();
62 return true;
63 }
64 return false;
65 }
66
67 @Override
68 public void onLongPress(MotionEvent arg0) {
69 // TODO Auto-generated method stub
70
71 }
72
73 @Override
74 public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
75 float arg3) {
76 // TODO Auto-generated method stub
77 return false;
78 }
79
80 @Override
81 public void onShowPress(MotionEvent arg0) {
82 // TODO Auto-generated method stub
83
84 }
85
86 @Override
87 public boolean onSingleTapUp(MotionEvent arg0) {
88 // TODO Auto-generated method stub
89 return false;
90 }
91 }

posted @ 2011-01-28 18:18  尚尚是高尚的尚  阅读(4666)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3