LayoutTransition 容器布局动画

在Property Animation中还有一个知识点没有讲到,那就是LayoutTransition容易布局动画,什么是容器布局动画?容器布局动画就是当一个布局

容器中的view方式改变时所产生的动画,比如:但一个相对布局中新增加一个view时或者删除一个view时,那么就可以通过一个动画来进行表现,

如:这个view是以什么形式增加进来或者删除出去。

android中提供了四种容器动画:

1、APPEARING: 动画所运行的项目出现在这个容器中时,即:view显示时的动画

2、CHANGE_APPEARING: 由于在这个容器总新增加了一个view,而导致原来的view位置发生改变所以会触发这个动画。

3、DISAPPEARING: view在这个容器中消失时触发的动画

4、CHANGE_DISAPPEARING: 由于在这个容器中移除了一个view,而导致原来的view位置发生改变所以会触发这个动画。

那么怎样使用呢?

1、创建LayoutTransition对象mTransitioner

2、创建动画

3、在xml文件中将相应布局的属性android:animateLayoutChanges="true"设置为true

4、将动画通过mTransitioner的setAnimator方法设置给mTransitioner

5、通过布局控件的setLayoutTransition方法将mTransitioner设置进去

下面就介绍其具体使用方法

	public void serTransition(){	
		mTransitioner = new LayoutTransition();
		setupCustomAnimations();
		ll.setLayoutTransition(mTransitioner);		
	}
	
    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(mTransitioner.getDuration(LayoutTransition.CHANGE_APPEARING));
        
        mTransitioner.setAnimator(LayoutTransition.CHANGE_APPEARING, changeIn);
        changeIn.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                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(mTransitioner.getDuration(LayoutTransition.CHANGE_DISAPPEARING));
        
        mTransitioner.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(mTransitioner.getDuration(LayoutTransition.APPEARING));
        mTransitioner.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(mTransitioner.getDuration(LayoutTransition.DISAPPEARING));
        mTransitioner.setAnimator(LayoutTransition.DISAPPEARING, animOut);
        animOut.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotationX(0f);
            }
        });
    }

参考链接:http://www.cnblogs.com/mengdd/p/3305973.html

你可能感兴趣的:(android,开发,android容器动画,property,animation,android,animation,动画)