package com.loaderman.customviewdemo; import android.animation.Keyframe; import android.animation.LayoutTransition; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { private LinearLayout linearLayoutContainer; private int i = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayoutContainer = (LinearLayout) findViewById(R.id.linearlayoutcontainer); // LayoutTransition transition = new LayoutTransition(); // //入场动画:view在这个容器中消失时触发的动画 // ObjectAnimator animIn = ObjectAnimator.ofFloat(null, "rotationY", 0f, 360f, 0f); // transition.setAnimator(LayoutTransition.APPEARING, animIn); // // //出场动画:view显示时的动画 // ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "rotation", 0f, 90f, 0f); // transition.setAnimator(LayoutTransition.DISAPPEARING, animOut); // // PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", 0, 0); // PropertyValuesHolder pvhTop = PropertyValuesHolder.ofInt("top", 0, 0); // PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f); // Animator changeAppearAnimator // = ObjectAnimator.ofPropertyValuesHolder(linearLayoutContainer, pvhLeft, pvhTop, pvhScaleX); // transition.setAnimator(LayoutTransition.CHANGE_APPEARING, changeAppearAnimator); LayoutTransition transition = new LayoutTransition(); PropertyValuesHolder outLeft = PropertyValuesHolder.ofInt("left", 0, 0); PropertyValuesHolder outTop = PropertyValuesHolder.ofInt("top", 0, 0); Keyframe frame0 = Keyframe.ofFloat(0f, 0); Keyframe frame1 = Keyframe.ofFloat(0.1f, -20f); Keyframe frame2 = Keyframe.ofFloat(0.2f, 20f); Keyframe frame3 = Keyframe.ofFloat(0.3f, -20f); Keyframe frame4 = Keyframe.ofFloat(0.4f, 20f); Keyframe frame5 = Keyframe.ofFloat(0.5f, -20f); Keyframe frame6 = Keyframe.ofFloat(0.6f, 20f); Keyframe frame7 = Keyframe.ofFloat(0.7f, -20f); Keyframe frame8 = Keyframe.ofFloat(0.8f, 20f); Keyframe frame9 = Keyframe.ofFloat(0.9f, -20f); Keyframe frame10 = Keyframe.ofFloat(1, 0); PropertyValuesHolder mPropertyValuesHolder = PropertyValuesHolder.ofKeyframe("rotation", frame0, frame1, frame2, frame3, frame4, frame5, frame6, frame7, frame8, frame9, frame10); ObjectAnimator mObjectAnimatorChangeDisAppearing = ObjectAnimator.ofPropertyValuesHolder(this, outLeft, outTop, mPropertyValuesHolder); transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, mObjectAnimatorChangeDisAppearing); transition.addTransitionListener(new LayoutTransition.TransitionListener() { public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) { Log.d("loaderman", "start:" + "transitionType:" + transitionType + "count:" + container.getChildCount() + "view:" + view.getClass().getName()); } public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) { Log.d("loaderman", "end:" + "transitionType:" + transitionType + "count:" + container.getChildCount() + "view:" + view.getClass().getName()); } }); linearLayoutContainer.setLayoutTransition(transition); findViewById(R.id.add_btn).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { addButtonView(); } }); findViewById(R.id.remove_btn).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { removeButtonView(); } }); } private void addButtonView() { i++; Button button = new Button(this); button.setText("button" + i); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); button.setLayoutParams(params); linearLayoutContainer.addView(button, 0); } private void removeButtonView() { if (i > 0) { linearLayoutContainer.removeViewAt(0); } i--; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加控件"/> <Button android:id="@+id/remove_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="移除控件"/> </LinearLayout> <LinearLayout android:id="@+id/linearlayoutcontainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:animateLayoutChanges="false" android:orientation="vertical"/> </LinearLayout>
效果:
Android提供四种方法为viewgroup组件添加动画
1.LayoutAnimation
和LayoutAnimationControllerz主要针对listview
2.gridLayoutAnimation主要针对gridview
3.android:animateLayoutChanges 属性 xml布局设置为true则带有默认动画,动画不能自定义
4.layoutTranstion最为强大,动画可以自定义
void setAnimator(int transitionType,Animator animator) 其中:transition取值如下
- LayoutTransition.APPEARING :元素在容器中出现时所定义的动画
- LayoutTransition.DISAPPEARING : 元素在容器中消失时所定义的动画
- LayoutTransition.CHANGE_APPEARING : 由于元素中显示新的元素,其他需要变化的元素所对应的动画
- LayoutTransition.CHANGE_DISAPPEARING: : 同上,相反,某个元素消失时,所对应的元素动画