LayoutAnimationController位于包android.view.animation下,用于控制一个ViewGroup下所有的子iew的补间动画。可以对所有的子view调用同一个补间动画,但是可以控制不同的子view响应动画的延迟时间。

A layout animation controller is used to animated a layout's, or a view group's, children. Each child uses the same animation but for every one of them, the animation starts at a different time. A layout animation controller is used byViewGroupto compute the delay by which each child's animation start must be offset. The delay is computed by using characteristics of each child, like its index in the view group. This standard implementation computes the delay by multiplying a fixed amount of miliseconds by the index of the child in its parent view group. Subclasses are supposed to overridegetDelayForView(android.view.View)to implement a different way of computing the delay. For instance, aGridLayoutAnimationControllerwill compute the delay based on the column and row indices of the child in its parent view group. Information used to compute the animation delay of each child are stored in an instance ofLayoutAnimationController.AnimationParameters, itself stored in theViewGroup.LayoutParamsof the view.

需要注意到的一个方法是:

public voidsetDelay(float delay)

childanimationdelay=childindex*delay*animationduration

设置view的延迟时间,参数是一个比例,而不是延迟的时间,需要注意一下。

下面是一个简单的例子:

packagecom.app.layoutanimation;importandroid.app.Activity;importandroid.os.Bundle;importandroid.util.DisplayMetrics;importandroid.util.TypedValue;importandroid.view.View;importandroid.view.ViewGroup;importandroid.view.animation.Animation;importandroid.view.animation.BounceInterpolator;importandroid.view.animation.LayoutAnimationController;importandroid.view.animation.TranslateAnimation;importandroid.widget.Button;publicclassMainActivityextendsActivityimplementsView.OnClickListener{/***Calledwhentheactivityisfirstcreated.*/ViewGroupmContainer=null;ButtonmButton=null;privateintmViewChildHeight=60;privateLayoutAnimationControllerlayoutController=null;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);mContainer=(ViewGroup)this.findViewById(R.id.container);mButton=(Button)this.findViewById(R.id.btn);mButton.setOnClickListener(this);DisplayMetricsoutMetris=newDisplayMetrics();this.getWindowManager().getDefaultDisplay().getMetrics(outMetris);//设置view的弹出高度mViewChildHeight=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,mViewChildHeight,outMetris);android.util.Log.d("--tag","mViewChildHeight:"+mViewChildHeight);startCustomAnimation();}privatevoidstartCustomAnimation(){//设置位移动画TranslateAnimationmTranlateAnimation=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF,0.0f);mTranlateAnimation.setDuration(1000);mTranlateAnimation.setInterpolator(newBounceInterpolator());//设置LayoutAnimationController的主要代码layoutController=newLayoutAnimationController(mTranlateAnimation);layoutController.setOrder(LayoutAnimationController.ORDER_NORMAL);layoutController.setDelay(0.2f);mContainer.setLayoutAnimation(layoutController);}@OverridepublicvoidonClick(Viewv){mContainer.startLayoutAnimation();}}

布局文件:

<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#FFDEAD"><Buttonandroid:layout_width="wrap_content"android:layout_height="50dp"android:layout_centerInParent="true"android:text="animation"android:id="@+id/btn"/><LinearLayoutandroid:id="@+id/container"android:layout_width="match_parent"android:layout_height="80dp"android:layout_alignParentBottom="true"android:gravity="bottom"android:weightSum="4"><ImageViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_weight="1"android:layout_marginBottom="5dp"android:src="@drawable/amazon"/><ImageViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_weight="1"android:layout_marginBottom="5dp"android:src="@drawable/androids"/><ImageViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_weight="1"android:layout_marginBottom="5dp"android:src="@drawable/bee"/><ImageViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_weight="1"android:layout_marginBottom="5dp"android:src="@drawable/google"/></LinearLayout></RelativeLayout>

最后的执行结果:


更多相关文章

  1. Android实现气泡布局/弹窗效果 气泡尖角方向及偏移量可控
  2. 浅析Android(安卓)Dialog中setContentView()方法
  3. Android(安卓)ListView单选CheckBox
  4. [置顶] Android常用UI控件之PopupWindow
  5. Android(安卓)自定义View (一)
  6. Android(安卓)Studio 中Kotlinx开发
  7. 如何实现Android(安卓)布局背景模糊化处理
  8. Android常用三方框架
  9. View高度动画

随机推荐

  1. Android O 物理键盘和虚拟键盘同时支持
  2. android layer-list
  3. Android Mediacodec H.265文件播放
  4. ListView多次调用getView方法
  5. Android 应用程序运行shell命令
  6. Android稳定性测试工具Monkey的使用
  7. Calculator-Android
  8. android java 执行shell命令(笔记)
  9. Android学习 数据存储之_文件存储
  10. [置顶] 通过SIM卡获取GPS,android基站定位