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

时间:2017-04-18 13:15:01

标签: android android-animation android-constraintlayout

我的布局如下:

<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_constraintHorizo​​ntal_bias param?

我试过了:

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

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

2 个答案:

答案 0 :(得分:2)

我希望,它可以帮到你。

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

答案 1 :(得分:1)

使用TransitionManager

  TransitionManager.beginDelayedTransition(constraintLayout)
  mAvatarWrapper.layoutParams = newParams

还可以用于更改边距等。