ConstraintLayout指南百分比动画

时间:2018-03-20 07:22:52

标签: java android android-constraintlayout

我在使用ConstraintLayout(java)时相当新。

我想要实现的是像小册子作为幻灯片动画一样显示/隐藏的时候,我尝试过这样的事情:

Animation a = new Animation() {
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        ConstraintLayout.LayoutParams lparams = (ConstraintLayout.LayoutParams) guideline.getLayoutParams();
        lparams.guidePercent = 0.5f;
        guideline.setLayoutParams(lparams);
    }
};
a.setDuration(3000);
a.setFillAfter(true);
guideline.startAnimation(a);

是的,指南(以及附加到它的相应视图)正在移动到屏幕的中心,但它并不像预期的那样平滑。有没有办法实现动画的流畅度?

任何建议都会非常感谢!

2 个答案:

答案 0 :(得分:2)

您可以使用ValueAnimator

Kotlin中的样本。

    val guideline = findViewById<View>(R.id.guideline2) as Guideline
    val end = (guideline.layoutParams as ConstraintLayout.LayoutParams).guidePercent
    // get end percent. start at 0
    val valueAnimator = ValueAnimator.ofFloat(0f, end)
    valueAnimator.duration = 3000
    // set duration 
    valueAnimator.interpolator = AccelerateDecelerateInterpolator()
    // set interpolator and  updateListener to get the animated value
    valueAnimator.addUpdateListener { valueAnimator ->
        val lp = guideline.layoutParams as ConstraintLayout.LayoutParams
        // get the float value
        lp.guidePercent = valueAnimator.animatedValue as Float
        // update layout params
        guideline.layoutParams = lp
    }
    valueAnimator.start()

Java版

 final Guideline guideline = findViewById(R.id.guideline2) ;
 ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams)guideline.getLayoutParams();
 float end =lp.guidePercent;
 ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, end);
 valueAnimator.setDuration(3000);
 valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams)guideline.getLayoutParams();
                lp.guidePercent = valueAnimator.getAnimatedFraction();
                guideline.setLayoutParams(lp);

            }
 });

答案 1 :(得分:1)

尝试使用$("#checkbox-container :checkbox");设置动画的动画效果。请参阅Beautiful animations using Android ConstraintLayout

  

[T]这是ConstraintLayout的另一个好处,大多数人都不知道,官方文档奇怪地没有提到任何事情:用很少的代码在ConstraintLayout视图上执行很酷的动画。

此技术还有其他来源和一些视频。我认为你会发现你想要做的事情更顺畅。