Android - 如何为约束布局参数设置动画?

Android - How to animate constraint layout params?

我的布局如下:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

...

    <FrameLayout
            android:id="@+id/wrapper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_bias=".5"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:padding="@dimen/glow">

        </FrameLayout>

</android.support.constraint.ConstraintLayout>

如何为 app:layout_constraintHorizontal_bias 参数设置动画?

我试过:

ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
        animation.setDuration(1000);
        animation.start();
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator updatedAnimation) {

                float animatedValue = (float)updatedAnimation.getAnimatedValue();

                ConstraintLayout.LayoutParams constraintLayoutParams = (ConstraintLayout.LayoutParams) mAvatarWrapper.getLayoutParams();
                constraintLayoutParams.horizontalBias = animatedValue;

            }
        });

或:

 public class ResizeAnimation extends Animation {
        View view;
        float horizontalBias;
        public ResizeAnimation(View view, float horizontalBias) {
            this.view = view;
            this.horizontalBias = horizontalBias;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            ((ConstraintLayout.LayoutParams) view.getLayoutParams()).horizontalBias = (horizontalBias * interpolatedTime);
            view.requestLayout();
        }

        @Override
        public void initialize(int width, int height, int parentWidth, int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    }

    ResizeAnimation resizeAnimation = new ResizeAnimation(
                mAvatarWrapper,1.0f

    );
    resizeAnimation.setDuration(1000);
    mAvatarWrapper.startAnimation(resizeAnimation);

没有任何成功。有人可以告诉我怎么做吗?

希望对你有所帮助

ConstraintLayout.LayoutParams constraintLayoutParams = (ConstraintLayout.LayoutParams) mAvatarWrapper.getLayoutParams();
constraintLayoutParams.horizontalBias = animatedValue;
TransitionManager.beginDelayedTransition(constraintLayout)
mAvatarWrapper.setLayoutParams(constraintLayoutParams);

使用 TransitionManager.

  TransitionManager.beginDelayedTransition(constraintLayout)
  mAvatarWrapper.layoutParams = newParams

也适用于更改边距等。