2017-04-18 63 views
2

我有布局如下所示:Android - 如何动画约束布局参数?

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

我如何可以动画应用程序: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

我希望它可以帮助你。

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

我真的很笨!,非常感谢) – phnmnn

+0

愚蠢的错误发生......欢迎您:) – CodeCameo