欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android开发 之 viewGroup视图动画

程序员文章站 2022-03-16 21:41:35
...

视图组添加,移除,隐藏,显示动画

ViewGroup ,LinearLayout,FrameLayout ,RelativeLayout等都是视图组,在他们添加,删除,显示隐藏的子view的时候的动画效果

如果想使用系统默认的动画效果只需添加一个属性
<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/verticalContainer"
    android:animateLayoutChanges="true" />

当然也可以自定义动画效果:

您可以使用LayoutTransition类在ViewGroup中对动画布局进行更改。 ViewGroup中的视图可以在您将视图添加到ViewGroup或将其从ViewGroup中删除时或当您使用VISIBLE,INVISIBLE或GONE调用View的setVisibility()方法时,看到并消失的动画。 当您添加或删除视图时,ViewGroup中的剩余视图也可以动画进入新的位置。 您可以通过调用setAnimator()并传递具有以下LayoutTransition常量之一的Animator对象来在LayoutTransition对象中定义以下动画:
APPEARING - 指示在容器中出现的项目上运行的动画的标志。(添加view自身的动画)
CHANGE_APPEARING - 指示由于容器中出现新项目而正在更改的项目上运行的动画的标志。(添加view时,其他view的移动动画)
DISAPPEARING - 指示在从容器中消失的项目上运行的动画的标志。(移除view自身的动画)
CHANGE_DISAPPEARING - 指示由于项目从容器中消失而正在更改的项目上运行的动画的标志。(移除view时,其他view的动画)

LayoutTransition transition=new  LayoutTransition();
transition.setAnimator(LayoutTransition.APPEARING,自定义的animator);
viewgroup.setLayoutTransition(transition);

自定义动画:可以参考apidemo中的LayoutAnimations示例
 private void createCustomAnimations(LayoutTransition transition) {
        // 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);
        customChangingAppearingAnim = ObjectAnimator.ofPropertyValuesHolder(
                        this, pvhLeft, pvhTop, pvhRight, pvhBottom, pvhScaleX, pvhScaleY).
                setDuration(transition.getDuration(LayoutTransition.CHANGE_APPEARING));
        customChangingAppearingAnim.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setScaleX(1f);
                view.setScaleY(1f);
            }
        });


        // 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);
        customChangingDisappearingAnim = ObjectAnimator.ofPropertyValuesHolder(
                        this, pvhLeft, pvhTop, pvhRight, pvhBottom, pvhRotation).
                setDuration(transition.getDuration(LayoutTransition.CHANGE_DISAPPEARING));
        customChangingDisappearingAnim.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotation(0f);
            }
        });

        // Adding  以y走旋转出现
        customAppearingAnim = ObjectAnimator.ofFloat(null, "rotationY", 90f, 0f).
                setDuration(transition.getDuration(LayoutTransition.APPEARING));
        customAppearingAnim.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotationY(0f);
            }
        });

        // Removing
        customDisappearingAnim = ObjectAnimator.ofFloat(null, "rotationX", 0f, 90f).
                setDuration(transition.getDuration(LayoutTransition.DISAPPEARING));
        customDisappearingAnim.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotationX(0f);
            }
        });
    }