Android ApiDemos示例解析(97):Views->Animation->Push

来源:互联网 发布:网络零食 编辑:程序博客网 时间:2024/05/18 00:26

在例子Android ApiDemos示例解析(95):View->Animation->3D Transition 为了在FrameLayout切换两个View :ListView 和 ImageView, 需要编写很多代码来实现两个View之间的切换。

Android 其实提供了一个更为简便的类ViewFlipper ,ViewFlipper既是FrameLayout 的子类,又是ViewAnimator 的子类,因此在功能上ViewFlipper 和FrameLayout类似,但它可以自动管理包含在ViewFlipper在各个子类之间的切换,而View之间的切换的动画效果可以通过ViewAnimator的功能来实现。

ViewAnimator 运行定义两个Animation动作:

  • inAnimation: 当View显示时动画资源ID
  • outAnimation: 当View隐藏是动画资源ID。

ViewFlipper 自动切换包含在其中的View,同一时间只能显示一个View,在View之间切换时,以inAnimation 动画显示新出现的View,而以outAnimation 动画隐藏先前的View。

本例中ViewFlipper 中定义了四个TextView,每个TextView显示一行文字:

<ViewFlipper android:id=”@+id/flipper” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:flipInterval=”2000″ android:layout_marginBottom=”20dip” > <TextView android:layout_width=”match_parent” android:layout_height=”wrap_content” android:gravity=”center_horizontal” android:textSize=”26sp” android:text=”@string/animation_2_text_1″/> <TextView android:layout_width=”match_parent” android:layout_height=”wrap_content” android:gravity=”center_horizontal” android:textSize=”26sp” android:text=”@string/animation_2_text_2″/> <TextView android:layout_width=”match_parent” android:layout_height=”wrap_content” android:gravity=”center_horizontal” android:textSize=”26sp” android:text=”@string/animation_2_text_3″/> <TextView android:layout_width=”match_parent” android:layout_height=”wrap_content” android:gravity=”center_horizontal” android:textSize=”26sp” android:text=”@string/animation_2_text_4″/> </ViewFlipper>

调用ViewFlipper 的 startFlipping() 开始以相同的间隔顺序显示四个TextView。

mFlipper.startFlipping();

提供一个Spinner(下拉框)允许设置不同inAnimation 和outAnimation 为TextView显示隐藏添加不同的动画效果,如:左边出左边进

mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));