Search code examples
androidandroid-animationandroid-constraintlayout

Android - How to animate constraint layout params?


i have layout like the following:

<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>

How i can animate app:layout_constraintHorizontal_bias param?

i tried:

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;

            }
        });

or :

 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);

without any success. Can someone refer me how to do it?


Solution

  • I hope, it might help you.

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