简体   繁体   English

¿如何制作正确的动画? (没有XML的Android Java)

[英]¿How make the correct animation? (Android java without xml)

I have the next code: 我有下一个代码:

public void desaparecer(final LinearLayout parent, View hijo, int espaciofinal) {

        parent.removeView(hijo); //remote the view of the parent

        final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)parent.getChildAt(0).getLayoutParams(); //create the params

        final int espacioIni = parent.getChildAt(0).getHeight(); //i get the height from start

        ValueAnimator animator = ValueAnimator.ofInt(params.height, espaciofinal); //pass the params and the final height

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {

                params.height = (Integer) valueAnimator.getAnimatedValue();
                parent.getChildAt(0).requestLayout(); //request
            }
        });
        animator.setDuration(300); //set the anim duration   

        animator.start(); //start the animation

    }

The animation don't work well. 动画效果不佳。 The idea would be that start the animation with the initial height. 想法是从初始高度开始动画。 (This would be the correct way) (这是正确的方法)

But, by reasons that i don't understand, the height (when start the animation) pass to be 0 and i need that don't destroy the original height. 但是,由于我不了解的原因,高度(开始动画时)传递为0,我需要不要破坏原始高度。

Any idea? 任何想法? I accept all ideas, except xml sources. 我接受所有想法,除了xml源。 I can't use it by reasons of my team. 由于团队原因,我无法使用它。

Thx. 谢谢。

For animation of changes on a layout you can set animateLayoutChanges="true" to ViewGroup item. 对于布局上的更改动画,您可以将animateLayoutChanges =“ true”设置为ViewGroup项目。 And any changes will be animated with default animation. 并且所有更改都将使用默认动画进行动画处理。 If you want to change the animation behavior you can set to ViewGroup your oun LayoutTransition object. 如果要更改动画行为,可以将LayoutTransition对象设置为ViewGroup。 Here google talks video which can help you, I hope ) Google在这里讲视频 ,希望对您有帮助)

solved: 解决了:

final int espacioIni = parent.getChildAt(2).getHeight();

    params.height = espacioIni;

    ValueAnimator animator = ValueAnimator.ofInt(params.height,espacio);

into ofInt pass the correct parametres and work well (startParameter, finishParameter) 进入ofInt传递正确的参数并正常工作(startParameter,finishParameter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM