【问题标题】:How to animate to wrap_content?如何为 wrap_content 设置动画?
【发布时间】:2019-03-19 23:54:58
【问题描述】:

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

【问题讨论】:

  • 你尝试过 animateLayoutChanges 吗? proandroiddev.com/…
  • 是的,不是我要找的。这是在使用布局参数和默认动画。我需要使用ValuAnimation
  • 您是否要扩展原本隐藏的视图?
  • @noahutz 是的,RelativeLayout 将在 0 和 wrap_content 之间切换

标签: android android-animation


【解决方案1】:

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

【讨论】:

  • 正是我想要的,不知道这个存在!非常有用的答案!
【解决方案2】:

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

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

【讨论】:

  • 这对我来说适用于单个文本视图,但是当我在具有多个文本视图的线性布局上使用它时,动画高度大于实际的 wrap_content 高度!它动画到过大的高度,然后又恢复到实际高度。
  • 解决了!看到这个答案:stackoverflow.com/a/59818318/7812339
猜你喜欢
  • 1970-01-01
  • 2020-05-05
  • 2012-01-08
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多