8.布局動畫

關于ViewGroup中添加子View的統一入場/出場動畫

layoutAnimation:布局動畫

api1中就有的方法,所以添加的動畫時Animation。該屬性只對創建ViewGroup時,對其子View有動畫。已經創建過了該ViewGroup的話,再向其添加子View不會有動畫。
什么時候是創建的時候:在onCreat方法中(不清楚具體時間點),在代碼中生成的Viewgroup。

  • onCreat創建加載布局時:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromDegrees="0"
    android:toDegrees="270"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="1000"
    android:repeatMode = "reverse"
    android:repeatCount = "3"
    android:fillAfter = "true"
    >
</rotate>

// layoutAnimation標簽
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:delay="1"
    android:animationOrder="normal"
    android:animation="@anim/rotate_anim">
</layoutAnimation>

//定義在LinearLayout上,在該界面生成時,Button顯示動畫。但是,后面在LinearLayout中添加Button時,不再有動畫。
 <LinearLayout
        android:id="@+id/ll_tips_target_animation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutAnimation="@anim/layout_animation"
        android:tag="在xml中設置的layoutAnimation"
        android:orientation="vertical">
        <Button
            style="@style/base_button"
            android:text="ViewGroup初始化時,子View有動畫"/>
    </LinearLayout>
  • 代碼中動態設置layoutAnimation,添加View
        //代碼生成ViewGroup
        LinearLayout linear = new LinearLayout(this);

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
        LayoutAnimationController controller = new LayoutAnimationController(animation);
        controller.setDelay(1);
        //動畫模式,正常/倒敘/隨機
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        //設置layoutAnimation
        linear.setLayoutAnimation(controller);
        linear.setLayoutAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.e(TAG, "onAnimationStart: start" );
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.e(TAG, "onAnimationEnd: end" );
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.e(TAG, "onAnimationRepeat: repeat" );
            }
        });

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linear.setLayoutParams(params);
        //給該ViewGroup添加子View,子View會有動畫。
        addVeiw(linear,null);
        //創建的新ViewGroup添加,顯示出來
        llTargetAnim.addView(linear, 0);
  • 使用場景:

該屬性只有ViewGroup創建的時候才能有效果,所以不適合動態添加子View的操作顯示動畫。一般做界面顯示的時候的入場動畫,比如打開一個界面,多個固定不變的item有動畫的顯示出來。(進入設置界面,信息展示界面)。

android:animateLayoutChanges屬性:

Api11后,添加/移除子View時所帶的默認動畫,在Xml中設置。不能自定義動畫,只能使用默認的。所以,使用范圍較小。

        <LinearLayout
        android:animateLayoutChanges="true"
        />
image

LayoutTransition 布局容器動畫

animateLayoutChanges只能使用默認動畫,LayoutTransition可以自定義動畫,并且也不用非得創建ViewGroup時才能顯示動畫,后面添加子View也能顯示。

        LayoutTransition layoutTransition = new LayoutTransition();
        //添加的View顯示動畫
        ObjectAnimator animatorAdd = ObjectAnimator.ofFloat(null,"ScaleX",1.0f,1.5f,1.0f);
        animatorAdd.setDuration(1000);
        layoutTransition.setAnimator(LayoutTransition.APPEARING, animatorAdd);
        
        //添加View時,已經有的View顯示的動畫。但是demo沒有顯示該動畫。
        PropertyValuesHolder valuesHolder = PropertyValuesHolder.ofFloat("rotation",0f,22f,0f);
        ValueAnimator animatorOld = ObjectAnimator.ofPropertyValuesHolder(llTargetAnim, valuesHolder);
        animatorOld.setDuration(2000);
        layoutTransition.setAnimator(LayoutTransition.CHANGE_APPEARING, animatorOld);
        
        layoutTransition.addTransitionListener(new LayoutTransition.TransitionListener() {
            @Override
            public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
                Log.e(TAG, "startTransition: start" );
            }

            @Override
            public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
                Log.e(TAG, "endTransition: end" );
            }
        });
        llTargetAnim.setLayoutTransition(layoutTransition);
        addVeiw(llTargetAnim,"添加的View動畫和已有View的動畫");

LayoutTransition有很多坑,屬性值第一和最后一個什么時候必須一樣,什么時候必須用PropertyValuesHolder封裝屬性...不研究了,麻蛋就這樣.

資料

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容