在Android约束布局中关闭屏幕的动画视图

时间:2017-11-02 16:26:41

标签: android android-animation android-constraintlayout

我在Constraint Layout中有一个TextView。我试图以视图从顶部离开屏幕的方式进行动画制作。这就是我到目前为止所做的,

ConstraintSet constraintSet = new ConstraintSet();
ConstraintLayout layout = (ConstraintLayout)holder.mView;
constraintSet.clone(layout);

constraintSet.clear(R.id.txt_PackageTitle,ConstraintSet.TOP);
constraintSet.clear(R.id.txt_PackageDescription,ConstraintSet.TOP);  
constraintSet.clear(R.id.txt_PackageTitle,ConstraintSet.BOTTOM); 
constraintSet.clear(R.id.txt_PackageDescription,ConstraintSet.BOTTOM);

constraintSet.setMargin(R.id.txt_PackageTitle,ConstraintSet.TOP,-600); 
constraintSet.setMargin(R.id.txt_PackageDescription,ConstraintSet.TOP,-1200);

ChangeBounds transition = new ChangeBounds();
transition.setInterpolator(new BounceInterpolator());
transition.setDuration(600);                
TransitionManager.beginDelayedTransition(layout,transition);
                    constraintSet.applyTo(layout);

现在这段代码只将内容移动到视图的最顶层,它不会离开视图而消失。

如何使用约束布局执行此操作?

1 个答案:

答案 0 :(得分:6)

不要清除TextView的底部约束,而是尝试将其底部限制在ConstraintLayout的顶部,如下所示:

constraintSet.connect (R.id.txt_PackageTitle, 
            ConstraintSet.BOTTOM, 
            PARENT_ID, 
            ConstraintSet.TOP);

现在,当视图设置动画时,它应该从顶部边缘滑落。

ConstraintLayout不支持负边距,注明here