如何为 wrap_content 设置动画?

est*_*con 7 android android-animation

是否可以使用ValueAnimatorto制作动画wrap_content?这似乎只适用于常数值。

public static void valueAnimate(final View obj, int from, int to, Interpolator interpolator, long duration, long delay){

    ValueAnimator anim = ValueAnimator.ofInt(from, to);
    anim.setInterpolator(interpolator == null ? DEFAULT_INTERPOLATOR : interpolator);
    anim.setDuration(duration);
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            Integer value = (Integer) animation.getAnimatedValue();
            obj.getLayoutParams().height = value.intValue();
            obj.requestLayout();
        }
    });
    anim.setStartDelay(delay);
    anim.start();
}
Run Code Online (Sandbox Code Playgroud)

我怎么能将to参数传递为wrap_content?

Animator.valueAnimate(mapUtilsContainer, CURR_MAPUTILC_H, 800, OVERSHOOT, 300, 0);
Run Code Online (Sandbox Code Playgroud)

fla*_*me3 7

一个简单的解决方案是在包含组件的布局中使用 android:animateLayoutChanges 属性。这适用于 Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN (Android 4.3)。例如,我有一个 EditText 需要从某一高度更改为wrap_content。

  1. 将 android:animateLayoutChanges 属性添加到我的 ScrollView 中。

      < ScrollView
        ...
        android:animateLayoutChanges="true">
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将其添加到您的 onCreateView() 中

    scrollView.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)

然后你会看到 EditText 高度的变化相当平滑。

  1. 将EditText的高度更改为wrap_content的代码

      fun wrapText() {
          val layoutParams = this.layoutParams
          layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
          this.layoutParams = layoutParams
          isExpanded = false
      }
    
    Run Code Online (Sandbox Code Playgroud)


小智 5

你可以做这样的事情。传递最初设置为消失的视图。

public static void expand(final View view) {
    view.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final int targetHeight = view.getMeasuredHeight();

    // Set initial height to 0 and show the view
    view.getLayoutParams().height = 0;
    view.setVisibility(View.VISIBLE);

    ValueAnimator anim = ValueAnimator.ofInt(view.getMeasuredHeight(), targetHeight);
    anim.setInterpolator(new AccelerateInterpolator());
    anim.setDuration(1000);
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = (int) (targetHeight * animation.getAnimatedFraction());
            view.setLayoutParams(layoutParams);
        }
    });
    anim.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            // At the end of animation, set the height to wrap content
            // This fix is for long views that are not shown on screen
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
    });
    anim.start();
}
Run Code Online (Sandbox Code Playgroud)