本文翻译自ViewAnimator Tutorial With Example In Android Studio

在Android中,ViewAnimatorFrameLayout的一个子类,用来做Views之间的切换。它是一个变换控件的
元素,帮助我们在Views之间(如TextView, ImageView或者其他layout)添加变换。它有助于在屏幕view添加动画。ViewAnimator可以在两个及以上Views上平滑的切换,通过合适动画,提供从一个View到另外一个View变换的方式。

内容概要

  • 基本的ViewAnimator XML
  • ViewAnimator实现步骤
  • ViewAnimator重要方法
  • ViewAnimator的XML属性
  • 在AndroidStudio的ViewAnimator实例

基本ViewAnimator XML

    

ViewAnimator实现步骤

  1. 在类中通过findViewById()方法获取ViewAnimator的引用,或者动态创建一个对象
  2. 使用switcherid.addView()方法在ViewAnimator添加子Views
  3. 使用switcherid.setInAnimation()设置进入动画
  4. 使用switcherid.setOutAnimation()设置退出动画

ViewAnimator的重要方法

1、showNext()这个方法用于展示ViewAnimator的下一个view。正如我们前面讨论过的,viewanimator可以有两个或者更多的子视图,一次只显示一个子视图,所以这个方法用于展示下一个视图。下面我们在按钮上执行点击事件并调用showNext()方法来显示viewanimator中的下一个视图。

ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); //get the reference of ViewAnimatorButton btnNext=(Button) findViewById(R.id.buttonNext); //get the reference of Button// set Click event on next buttonbtnNext.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stub// show the next view of ViewAnimatorsimpleViewAnimator.showNext();}});

2、showPrevious()这个方法用于展示ViewAnimator的上一个view。正如我们前面讨论过的,viewanimator可以有两个或者更多的子视图,一次只显示一个子视图,所以这个方法用于展示上一个视图。下面我们在按钮上执行点击事件并调用showPrevious()方法来显示viewanimator中的下一个视图。

ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); // get the reference of ViewAnimatorButton btnPrevious=(Button) findViewById(R.id.buttonPrevious); // get the reference of Button// set Click event on next buttonbtnPrevious.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stub// show the next view of ViewFlippersimpleViewAnimator.showPrevious();}});

3、loadAnimation(Context context, int id)这个方法用于定义一个动画对象,通过调AnimationUtils AnimationUtils类的静态方法loadanimation。下面我们创建一个动画对象并且使用AnimationUtils类加载一个动画。

// Load Animation using AnimationUtils classAnimation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);

4、setInAnimation(in)这个方法用于设置对象进入屏幕的动画。下面我们创建一个动画对象,并且使用AnimationUtils加载一个动画,然后设置这个动画在ViewAnimator。

ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id.simpleViewAnimator); // initiate a ViewAnimatorAnimation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); // load an animationsimpleViewAnimator.setInAnimation(in); // set in Animation for ViewAnimator

5、setOutAnimation(out)这个方法和setInAnimation(in)相反。当我们显示下一个view时,它首先使用 setOutAnimation()设置的动画移除旧的view, 然后使用setInAnimation()设置的动画放置新的view。

ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id. simpleViewAnimator); // get reference of ViewAnimatorAnimation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); // load an animationsimpleViewAnimator.setOutAnimation(out); // set out Animation for ViewAnimator

6、addView(View child)这个方法用于运行时在ViewAnimator添加view。下面我们创建一个TextView,然后添加到我们的ViewAnimator。

ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id.simpleViewAnimator); // get reference of ViewAnimatorTextView textView = new TextView(this); // create a TextViewtextView.setText("View Animator TextView"); // set text in TextViewsimpleViewAnimator.addView(textView); // add the TextView in ViewAnimator   

7、getCurrentView()这个用于获取ViewAnimator当前显示的子view。下面我们获取ViewAnimator显示的子view。

ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); // get the reference of ViewAnimatorView view = simpleViewAnimator.getCurrentView(); // get current displayed child view of ViewAnimator

8、getDisplayedChild()这个方法用于获取ViewAnimator当前显示的子View的id,返回一个int值。下面是示例。

ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); // get the reference of ViewAnimatorint  displayedChildIndex = simpleViewAnimator.getDisplayedChild(); // get index for current displayed child view of ViewAnimator    

9、getInAnimation()此方法用于获取当前用于进入屏幕的View动画。这个方法返回我们通过setInAnimation()设置的进入动画。下面我们首先设置进入动画,然后获取当前进入屏幕的View动画。

ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id.simpleViewAnimator); // initiate a ViewAnimatorAnimation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); // load an animationsimpleViewAnimator.setInAnimation(in); // set in Animation for ViewAnimatorAnimation currentInAnimation = simpleViewAnimator.getInAnimation(); // get current animation that is used to animate a View that enters the screen.

10、getOutAnimation()此方法用于获取当前用于退出屏幕的View动画。这个方法返回我们通过setOutAnimation()设置的进入动画。下面我们首先设置退出动画,然后获取当前退出屏幕的View动画。

ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id. simpleViewAnimator); // get reference of ViewAnimatorAnimation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); // load an animationsimpleViewAnimator.setOutAnimation(out); // set out Animation for ViewAnimatorAnimation currentOutAnimation = simpleViewAnimator.getOutAnimation(); // get current animation that is used to animate a View that exits the screen.

11、removeAllViews()这个方法用于从ViewGroup移除所有的子view。下面是示例。

ViewAnimator simpleViewAnimator=(ViewAnimator)findViewById(R.id. simpleViewAnimator); // get reference of ViewAnimatorsimpleViewAnimator.removeAllViews(); // remove all the child views of ViewAnimator

12、removeView(View view)这个方法用于移除ViewAnimator的子View。在这个方法,我们传递了想要移除的子view对象。下面我们首先获取当前ViewAnimator显示的子view,然后从viewAnimator移除这个子view。

ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); // get the reference of ViewAnimatorView view=simpleViewAnimator.getCurrentView(); // get current displayed child view of ViewAnimatorsimpleViewAnimator.removeView(view); // remove the current displayed child view of ViewAnimator

13、removeViewAt(int index) 这个用于移除在布局里特定位置的view。在这个方法,我们传递想要移除的子view的id。下面我们首先获取ViewAnimator当前显示的子View的id,然后从ViewAnimator移除这个子view。

ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); // get the reference of ViewAnimatorint  displayedChildIndex = simpleViewAnimator.getDisplayedChild(); // get index for current displayed child view of ViewAnimatorsimpleViewAnimator.removeViewAt(displayedChildIndex); // remove the current displayed child view of ViewAnimator

14、setDisplayedChild(int whichChild)这个方法用于设置在ViewAnimator显示的子View。在这个方法里,我们设值将要显示的子View的id。

ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); // get the reference of ViewAnimatorsimpleViewAnimator.setDisplayedChild(1); // set the indez of current displayed child view of ViewAnimator

15、setAnimateFirstView(boolean animate)这个方法用于设置当前视图是否应在第一次显示ViewAnimator时进行动画。在这个方法里,我们设置true或者false。下面是示例。

ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); // get the reference of ViewAnimatorsimpleViewAnimator.setAnimateFirstView(true); // set true value for setAnimateFirstView

使用上面的示例将看到ViewAnimator的首个view进行动画,如果你设置为false,那么首次显示不会进行动画。

16、getAnimateFirstView()这个方法检查当前view是否在第一次viewanimator显示时进行动画。下面我们首先在setAnimateFirstView设置ture值,然后检查当前view是否在第一次显示ViewAnimator时进行动画。

ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); // get the reference of ViewAnimatorsimpleViewAnimator.setAnimateFirstView(true); // set true value for setAnimateFirstViewBoolean isAnimateFirstTime=simpleViewAnimator.getAnimateFirstView(); // checks whether the view should animate first time or not.

ViewAnimator的XML属性

现在我们讨论一些ViewAnimator的普通属性,帮助我们在布局xml中配置它。

1、Id属性用于ViewAnimator的唯一标识。

< ViewAnimatorandroid:id="@+id/simpleViewAnimator"android:layout_width="match_parent"android:layout_height="wrap_content" >  

5、paddinng这个属性用于设置ViewAnimator的左、右、上、下padding值。相关属性还有paddingRightpaddingLeftpaddingToppaddingBottom。下面是示例。

 

6、background这个属性用于设置ViewFipper的背景。我们可以在ViewAnimator的背景中设置一个color或者drawable。下面是示例。

 

更多相关文章

  1. android部分介绍
  2. 简易音乐播放器(Android(安卓)Studio)
  3. Android(安卓)support library支持包常用控件介绍(二)
  4. Windos下Android(ADT Bundle)配置NDK的两种方法------ADT、Cygwin
  5. Android最佳实践之Material Design
  6. Android(安卓)4.4堆叠结构的变化
  7. Android学习--深入探索RemoteViews
  8. Android事件分发机制的探索与发现之总结篇
  9. Android(安卓)Hook框架Xposed详解:从源代码分析到开发指南

随机推荐

  1. Android(安卓)进程内存、CPU使用查看
  2. android Activity生命周期详解(图文)
  3. android去除状态栏和下面视图之前的黑线
  4. android 命令大全
  5. Android(安卓)APK安装过程及原理详解
  6. android之4.0控件switch自定义开关滑块、
  7. Android下的Console命令
  8. 在Android中使用Timer,并创建一个应用程序
  9. 键盘自动弹出解决
  10. Android很有用的代码片段