android中LayoutAnimationController类的使用
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 byViewGroup
to 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, aGridLayoutAnimationController
will 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.LayoutParams
of 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>
最后的执行结果:
更多相关文章
- Android实现气泡布局/弹窗效果 气泡尖角方向及偏移量可控
- 浅析Android(安卓)Dialog中setContentView()方法
- Android(安卓)ListView单选CheckBox
- [置顶] Android常用UI控件之PopupWindow
- Android(安卓)自定义View (一)
- Android(安卓)Studio 中Kotlinx开发
- 如何实现Android(安卓)布局背景模糊化处理
- Android常用三方框架
- View高度动画