1. 程式人生 > >Android佈局動畫---給你的View閃亮登場

Android佈局動畫---給你的View閃亮登場

1、概述

首先。什麼是佈局動畫。就是ViewGroup在佈局時產生的動畫。(不僅僅侷限於新增View,也包括刪除View或者導致位置變化等一系列原因,我們都可以認為是佈局動畫)

2、LayoutTransition動畫

通過LayoutTransition,我們可以實現ViewGroup在新增子View時的動畫過度效果。過度動畫可以分為以下幾種型別

LayoutTransition.APPEARING 當一個View在ViewGroup中出現時,對此View設定的動畫

LayoutTransition.CHANGE_APPEARING當一個View在ViewGroup中出現時,對此View對其他View位置造成影響,對其他View設定的動畫

LayoutTransition.DISAPPEARING當一個View在ViewGroup中消失時,對此View設定的動畫

LayoutTransition.CHANGE_DISAPPEARING當一個View在ViewGroup中消失時,對此View對其他View位置造成影響,對其他View設定的動畫

LayoutTransition.CHANGE 不是由於View出現或消失造成對其他View位置造成影響,然後對其他View設定的動畫。

public class AnimateLayoutTransition extends Activity {
 
    private LinearLayout ll;
    private LayoutTransition mTransition = new LayoutTransition();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animate_layout_transition);
 
        ll = (LinearLayout) findViewById(R.id.ll);
        setupCustomAnimations();
        ll.setLayoutTransition(mTransition);
    }
 
    public void add(View view) {
        final Button button = new Button(this);
        ll.addView(button);
        button.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                ll.removeView(button);
            }
        });
    }
 
    // 生成自定義動畫
    private void setupCustomAnimations() {
        // 動畫:CHANGE_APPEARING
        // Changing while Adding
        PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", 0, 1);
        PropertyValuesHolder pvhTop = PropertyValuesHolder.ofInt("top", 0, 1);
        PropertyValuesHolder pvhRight = PropertyValuesHolder.ofInt("right", 0,
                1);
        PropertyValuesHolder pvhBottom = PropertyValuesHolder.ofInt("bottom",
                0, 1);
        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX",
                1f, 0f, 1f);
        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofFloat("scaleY",
                1f, 0f, 1f);
 
        final ObjectAnimator changeIn = ObjectAnimator.ofPropertyValuesHolder(
                this, pvhLeft, pvhTop, pvhRight, pvhBottom, pvhScaleX,
                pvhScaleY).setDuration(
                mTransition.getDuration(LayoutTransition.CHANGE_APPEARING));
        mTransition.setAnimator(LayoutTransition.CHANGE_APPEARING, changeIn);
        changeIn.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                // View也支援此種動畫執行方式了
                view.setScaleX(1f);
                view.setScaleY(1f);
            }
        });
 
        // 動畫:CHANGE_DISAPPEARING
        // Changing while Removing
        Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
        Keyframe kf1 = Keyframe.ofFloat(.9999f, 360f);
        Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
        PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(
                "rotation", kf0, kf1, kf2);
        final ObjectAnimator changeOut = ObjectAnimator
                .ofPropertyValuesHolder(this, pvhLeft, pvhTop, pvhRight,
                        pvhBottom, pvhRotation)
                .setDuration(
                        mTransition
                                .getDuration(LayoutTransition.CHANGE_DISAPPEARING));
        mTransition
                .setAnimator(LayoutTransition.CHANGE_DISAPPEARING, changeOut);
        changeOut.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotation(0f);
            }
        });
 
        // 動畫:APPEARING
        // Adding
        ObjectAnimator animIn = ObjectAnimator.ofFloat(null, "rotationY", 90f,
                0f).setDuration(
                mTransition.getDuration(LayoutTransition.APPEARING));
        mTransition.setAnimator(LayoutTransition.APPEARING, animIn);
        animIn.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotationY(0f);
            }
        });
 
        // 動畫:DISAPPEARING
        // Removing
        ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "rotationX", 0f,
                90f).setDuration(
                mTransition.getDuration(LayoutTransition.DISAPPEARING));
        mTransition.setAnimator(LayoutTransition.DISAPPEARING, animOut);
        animOut.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotationX(0f);
            }
        });
 
    }
}