Android animate layouts and views

Mointy :

I have a ConstraintLayout with a navigation on the bottom and a fragment container that takes up the rest of the screen. What I want is to programmatically add/remove a view between the navigation and the fragment container with animations.

I have this code to add and animate the new view:

//Adding the view to the main layout
ConstraintLayout layout = findViewById(R.id.ConstraintLayout);
getLayoutInflater().inflate(R.layout.newView, layout);

//Start animation immediately with a library
YoYo.with(Techniques.SlideInUp).duration(800).playOn(findViewById(R.id.newView));

Now the code above doesn't look solid for me. I would like to find a better way, but for now it works.

After adding the new view, I change the constraints of the fragment container from the top of the navigation to the top of the new view. The size of the fragment container is set to match_constrainst, so by changing the constraints it will now be a little bit smaller.

    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(layout);
    constraintSet.connect(R.id.fragment_container,ConstraintSet.BOTTOM,R.id.newViewWrapper,ConstraintSet.TOP,0);
    constraintSet.applyTo(layout);

What I didn't manage to do is to animate the size changing of the fragment container. And overall I never worked with animations in android.

Is there a better or best practice way of animating views and layouts in android or is there a library that can do it?

Legend Bard :

If you use Constraintlayout than you can use TransitionManager for your layout animations. The basic setup is like this:

TransitionManager.beginDelayedTransition(layout);

It will automatically animate the layout once you for example change the size of a view/constraint.

To change constraints you can use ConstraintSet.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=374059&siteId=1