本文翻译自ViewAnimator Tutorial With Example In Android Studio
在Android中,
ViewAnimator
是FrameLayout
的一个子类,用来做Views之间的切换。它是一个变换控件的
元素,帮助我们在Views之间(如TextView
,ImageView
或者其他layout)添加变换。它有助于在屏幕view添加动画。ViewAnimator
可以在两个及以上Views上平滑的切换,通过合适动画,提供从一个View到另外一个View变换的方式。
内容概要
- 基本的ViewAnimator XML
- ViewAnimator实现步骤
- ViewAnimator重要方法
- ViewAnimator的XML属性
- 在AndroidStudio的ViewAnimator实例
基本ViewAnimator XML
<ViewAnimator
android:id="@+id/simpleViewAnimator"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Add View’s Here -->
</ViewAnimator>
ViewAnimator实现步骤
- 在类中通过
findViewById()
方法获取ViewAnimator的引用,或者动态创建一个对象 - 使用
switcherid.addView()
方法在ViewAnimator添加子Views - 使用
switcherid.setInAnimation()
设置进入动画 - 使用
switcherid.setOutAnimation()
设置退出动画
ViewAnimator的重要方法
1、showNext()
这个方法用于展示ViewAnimator的下一个view。正如我们前面讨论过的,viewanimator可以有两个或者更多的子视图,一次只显示一个子视图,所以这个方法用于展示下一个视图。下面我们在按钮上执行点击事件并调用showNext()
方法来显示viewanimator中的下一个视图。
//get the reference of ViewAnimator
ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); //get the reference of Button Button btnNext=(Button) findViewById(R.id.buttonNext); // set Click event on next button btnNext.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub // show the next view of ViewAnimator simpleViewAnimator.showNext(); } });
2、showPrevious()
这个方法用于展示ViewAnimator的上一个view。正如我们前面讨论过的,viewanimator可以有两个或者更多的子视图,一次只显示一个子视图,所以这个方法用于展示上一个视图。下面我们在按钮上执行点击事件并调用showPrevious()
方法来显示viewanimator中的下一个视图。
// get the reference of ViewAnimator
ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); // get the reference of Button Button btnPrevious=(Button) findViewById(R.id.buttonPrevious); // set Click event on next button btnPrevious.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub // show the next view of ViewFlipper simpleViewAnimator.showPrevious(); } });
3、loadAnimation(Context context, int id)
这个方法用于定义一个动画对象,通过调AnimationUtils AnimationUtils
类的静态方法loadanimation。下面我们创建一个动画对象并且使用AnimationUtils
类加载一个动画。
// Load Animation using AnimationUtils class
Animation 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。
// initiate a ViewAnimator
ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id.simpleViewAnimator); // load an animation
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
// set in Animation for ViewAnimator simpleViewAnimator.setInAnimation(in);
5、setOutAnimation(out)
这个方法和setInAnimation(in)
相反。当我们显示下一个view时,它首先使用 setOutAnimation()设置的动画移除旧的view, 然后使用setInAnimation()设置的动画放置新的view。
ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id. simpleViewAnimator); Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
// set out Animation for ViewAnimator simpleViewAnimator.setOutAnimation(out);
6、addView(View child)
这个方法用于运行时在ViewAnimator
添加view。下面我们创建一个TextView,然后添加到我们的ViewAnimator。
ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id.simpleViewAnimator); // create a TextView TextView textView = new TextView(this);
// set text in TextView textView.setText("View Animator TextView");
// add the TextView in ViewAnimator simpleViewAnimator.addView(textView);
7、getCurrentView()
这个用于获取ViewAnimator
当前显示的子view。下面我们获取ViewAnimator显示的子view。
ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator);
// get current displayed child view of ViewAnimator View view = simpleViewAnimator.getCurrentView();
8、getDisplayedChild()
这个方法用于获取ViewAnimator
当前显示的子View的id,返回一个int值。下面是示例。
ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator);
// get index for current displayed child view of ViewAnimator int displayedChildIndex = simpleViewAnimator.getDisplayedChild();
9、getInAnimation()
此方法用于获取当前用于进入屏幕的View动画。这个方法返回我们通过setInAnimation()设置的进入动画。下面我们首先设置进入动画,然后获取当前进入屏幕的View动画。
ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id.simpleViewAnimator);
// load an animation Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
// set in Animation for ViewAnimator simpleViewAnimator.setInAnimation(in);
// get current animation that is used to animate a View that enters the screen. Animation currentInAnimation = simpleViewAnimator.getInAnimation();
10、getOutAnimation()
此方法用于获取当前用于退出屏幕的View动画。这个方法返回我们通过setOutAnimation()设置的进入动画。下面我们首先设置退出动画,然后获取当前退出屏幕的View动画。
ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id. simpleViewAnimator);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
simpleViewAnimator.setOutAnimation(out); // set out Animation for ViewAnimator
// get current animation that is used to animate a View that exits the screen.
Animation currentOutAnimation = simpleViewAnimator.getOutAnimation();
11、removeAllViews()
这个方法用于从ViewGroup移除所有的子view。下面是示例。
ViewAnimator simpleViewAnimator=(ViewAnimator)findViewById(R.id. simpleViewAnimator);
// remove all the child views of ViewAnimator
simpleViewAnimator.removeAllViews();
12、removeView(View view)
这个方法用于移除ViewAnimator的子View。在这个方法,我们传递了想要移除的子view对象。下面我们首先获取当前ViewAnimator显示的子view,然后从viewAnimator移除这个子view。
ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator);
// get current displayed child view of ViewAnimator View view=simpleViewAnimator.getCurrentView();
// remove the current displayed child view of ViewAnimator simpleViewAnimator.removeView(view);
13、removeViewAt(int index)
这个用于移除在布局里特定位置的view。在这个方法,我们传递想要移除的子view的id。下面我们首先获取ViewAnimator当前显示的子View的id,然后从ViewAnimator移除这个子view。
ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator);
// get index for current displayed child view of ViewAnimator int displayedChildIndex = simpleViewAnimator.getDisplayedChild();
// remove the current displayed child view of ViewAnimator simpleViewAnimator.removeViewAt(displayedChildIndex);
14、setDisplayedChild(int whichChild)
这个方法用于设置在ViewAnimator显示的子View。在这个方法里,我们设值将要显示的子View的id。
ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator);
// set the index of current displayed child view of ViewAnimator simpleViewAnimator.setDisplayedChild(1);
15、setAnimateFirstView(boolean animate)
这个方法用于设置当前视图是否应在第一次显示ViewAnimator时进行动画。在这个方法里,我们设置true或者false。下面是示例。
ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator);
// set true value for setAnimateFirstView simpleViewAnimator.setAnimateFirstView(true);
使用上面的示例将看到ViewAnimator的首个view进行动画,如果你设置为false,那么首次显示不会进行动画。
16、getAnimateFirstView()
这个方法检查当前view是否在第一次viewanimator显示时进行动画。下面我们首先在setAnimateFirstView设置ture值,然后检查当前view是否在第一次显示ViewAnimator时进行动画。
ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator);
// set true value for setAnimateFirstView simpleViewAnimator.setAnimateFirstView(true);
// checks whether the view should animate first time or not. Boolean isAnimateFirstTime=simpleViewAnimator.getAnimateFirstView();
ViewAnimator的XML属性
现在我们讨论一些ViewAnimator的普通属性,帮助我们在布局xml中配置它。
1、Id
属性用于ViewAnimator的唯一标识。
< ViewAnimator android:id="@+id/simpleViewAnimator" android:layout_width="match_parent" android:layout_height="wrap_content" >
<!-- id of the ViewAnimator used to uniquely identify it --> <!-- Add View’s Here -- > </ ViewAnimator >
2、animateFirstView
这个属性用于设置当前view是否在第一次显示ViewAnimator时进行动画。我们设置布尔值给这个属性。我们也可以通过setAnimateFirstView(boolean animate) 方法在代码中设置这个值。
下面是示例。
<ViewAnimator
android:id="@+id/simpleViewAnimator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateFirstView="true">
<!--set true value for animateFirstView attribute -->
<!-- Add View’s Here -- >
</ ViewAnimator >
3、inAnimation
这个属性用于显示view时的动画的标识符。下面是示例。
<ViewAnimator
android:id="@+id/simpleViewAnimator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateFirstView="true"
android:inAnimation="@android:anim/slide_in_left">
<!-- slide in left animation for the child views of ViewAnimator-->
<!-- Add View’s Here -- >
</ ViewAnimator >
4、outAnimation
这个方法用于隐藏view时的动画的标识符。下面是示例。
<ViewAnimator
android:id="@+id/simpleViewAnimator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateFirstView="true"
android:outAnimation="@android:anim/slide_out_right">
<!-- slide out right animation for the child views of ViewAnimator-->
<!--Add View’s Here-->
</ViewAnimator>
5、paddinng
这个属性用于设置ViewAnimator的左、右、上、下padding值。相关属性还有paddingRight
、paddingLeft
、paddingTop
、paddingBottom
。下面是示例。
<ViewAnimator
android:id="@+id/simpleViewAnimator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<!-- set 10 dp padding from all the sides of ViewAnimator -->
<!-- Add View’s Here-->
</ViewAnimator>
6、background
这个属性用于设置ViewFipper的背景。我们可以在ViewAnimator的背景中设置一个color或者drawable。下面是示例。
<ViewAnimator
android:id="@+id/simpleViewAnimator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00">
<!-- set red color in the background of ViewFlipper -->
<!-- Add View’s Here -- >
</ViewAnimator >
在代码中设置backgound如下
ViewAnimator simpleViewAnimator = (ViewAnimator)findViewById(R.id. simpleViewAnimator);
// set red color in the background of ViewAnimator simpleViewAnimator.setBackgroundColor(Color.RED);
在AndroidStudio的ViewAnimator实例
在这个ViewAnimator的Demo,我们显示了一个绑定了5张图片的ViewAnimator。首先我们创建一个图片的数组,然后用addView
方法在添加图片到ViewAnimator。同时我们也使用一个Button来切换ViewAnimator的view。
我们在ViewAnimator加载设置从左边滑进和从右边滑出的动画。然后当用户点击button时,ViewAnimator切换不同的views,可以看到当前的view以指定的动画滑出、下一个view以指定的动画滑进。我们也设置false值给setAnimateFirstView
,从而关闭第一个view首次显示时的动画。