java.lang.Object | |||||
↳ | android.view.View | ||||
↳ | android.view.ViewGroup | ||||
↳ | android.widget.FrameLayout | ||||
↳ | android.widget.ViewAnimator | ||||
↳ | android.widget.ViewFlipper |
ViewFlipper 继承自 ViewAnimator ,而 ViewAnimator 继承自 FrameLayout 。FrameLayout的特点是它的子View是叠加在一起的,由此可以知道ViewAnimator以及ViewFlipper都有这个特性。ViewAnimator 会在它的子View 切换时播放动画。
1.ViewAnimator 有两个成员变量。
mInAnimation,mOutAnimation,顾名思义就是切换时的动画。
1 Animation mInAnimation;
2 Animation mOutAnimation;
2.当要切换两个View时调用如下两个方法:
1 /**
2 * Manually shows the next child.
3 */
4 public void showNext() {
5 setDisplayedChild(mWhichChild + 1);
6 }
7
8 /**
9 * Manually shows the previous child.
10 */
11 public void showPrevious() {
12 setDisplayedChild(mWhichChild - 1);
2 * Manually shows the next child.
3 */
4 public void showNext() {
5 setDisplayedChild(mWhichChild + 1);
6 }
7
8 /**
9 * Manually shows the previous child.
10 */
11 public void showPrevious() {
12 setDisplayedChild(mWhichChild - 1);
13 }
使用方法
1.定义动画left_to_right_in , left_to_right_out , right_to_left_in , right_to_left_out ;
1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
3
4 <translate
5 android:duration="100"
6 android:fromXDelta="-100%p"
7 android:toXDelta="0" />
8
2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
3
4 <translate
5 android:duration="100"
6 android:fromXDelta="-100%p"
7 android:toXDelta="0" />
8
9 </set>
2.在布局中使用ViewFlipper,并且添加几个子View;
1 <ViewFlipper
2 android:id="@+id/viewFlipper"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:background="#8000ff"
11 android:text="第一页" />
12
13 <TextView
14 android:layout_width="fill_parent"
15 android:layout_height="fill_parent"
16 android:background="#808000"
17 android:text="第二页" />
18
19 <TextView
20 android:layout_width="fill_parent"
21 android:layout_height="fill_parent"
22 android:background="#ffff00"
23 android:text="第三页" />
24
25 <TextView
26 android:layout_width="fill_parent"
27 android:layout_height="fill_parent"
28 android:background="#008040"
29 android:text="第四页" />
30
31 <TextView
32 android:layout_width="fill_parent"
33 android:layout_height="fill_parent"
34 android:background="#FF8000"
35 android:text="第五页" />
36 </ViewFlipper>
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:background="#8000ff"
11 android:text="第一页" />
12
13 <TextView
14 android:layout_width="fill_parent"
15 android:layout_height="fill_parent"
16 android:background="#808000"
17 android:text="第二页" />
18
19 <TextView
20 android:layout_width="fill_parent"
21 android:layout_height="fill_parent"
22 android:background="#ffff00"
23 android:text="第三页" />
24
25 <TextView
26 android:layout_width="fill_parent"
27 android:layout_height="fill_parent"
28 android:background="#008040"
29 android:text="第四页" />
30
31 <TextView
32 android:layout_width="fill_parent"
33 android:layout_height="fill_parent"
34 android:background="#FF8000"
35 android:text="第五页" />
36 </ViewFlipper>
3.设置in和out动画 ;
1 private void previousAnimation() {
2 mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
3 R.anim.left_to_right_in));
4 mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
5 R.anim.left_to_right_out));
6 mViewFlipper.showPrevious();
7 }
8
9 private void nextViewAnimation() {
10 mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
11 R.anim.right_to_left_in));
12 mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
13 R.anim.right_to_left_out));
14 mViewFlipper.showNext();
2 mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
3 R.anim.left_to_right_in));
4 mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
5 R.anim.left_to_right_out));
6 mViewFlipper.showPrevious();
7 }
8
9 private void nextViewAnimation() {
10 mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
11 R.anim.right_to_left_in));
12 mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
13 R.anim.right_to_left_out));
14 mViewFlipper.showNext();
15 }
效果图
详细的代码实现方式请查看附件