如何动画到 wrap_content?

How to animate to wrap_content?

是否可以使用 ValueAnimatorwrap_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();
}

如何将 to 参数作为 wrap_content 传递?

Animator.valueAnimate(mapUtilsContainer, CURR_MAPUTILC_H, 800, OVERSHOOT, 300, 0);

你可以这样做。传递最初设置为 gone 的视图。

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

一个简单的解决方案是在包含组件的布局中使用 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">
    
  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
      }