[英]Android animate layouts and views
I have a ConstraintLayout
with a navigation on the bottom and a fragment container that takes up the rest of the screen.我有一个ConstraintLayout
,底部有一个导航,一个片段容器占据了屏幕的其余部分。 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.片段容器的大小设置为 match_constrainst,因此通过更改约束,它现在会变小一点。
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.总的来说,我从未在 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?在android中是否有更好或最佳实践的动画视图和布局方法,或者是否有可以做到这一点的库?
If you use Constraintlayout
than you can use TransitionManager
for your layout animations.如果您使用Constraintlayout
,则可以将TransitionManager
用于布局动画。 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
.要更改约束,您可以使用ConstraintSet
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.