【问题标题】:How to animate TextView height change如何动画 TextView 高度变化
【发布时间】:2016-08-23 16:11:04
【问题描述】:

我在 LinearLayout 中有一个 TextView。我的目标是在 TextView 的文本发生变化并且需要更多或更少的行时为它的高度变化设置动画。

我是这样拆分工作的:

  1. 淡出旧文本
  2. 为 TextView 的新高度(和父级 LinearLayout)设置动画
  3. 淡入新文本

淡入淡出部分很简单,但我为高度变化动画而努力。

这是我的简化布局:

<LinearLayout
    android:id="@+id/fragment_tooltip_tooltip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/tooltip_blue_bg"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/fragment_tooltip_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

这是我尝试过的:

    final int currentHeight = tvMessage.getHeight();

    tvMessage.setText(toTooltip.getMessage());

    tvMessage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            TooltipFragment.this.tvMessage.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            final int newHeight = tvMessage.getHeight();

            ResizeAnimation resizeAnimation = new ResizeAnimation(tvMessage, tvMessage.getWidth(), tvMessage.getWidth(),
                    currentHeight, newHeight);
            resizeAnimation.setDuration(ANIM_DURATION_TRANSITION_TEXT_RESIZE);
            resizeAnimation.setInterpolator(new FastOutSlowInInterpolator());
            resizeAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    // No-op
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // Finally we show the new content
                    ...
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // No-op
                }
            });
            tvMessage.startAnimation(resizeAnimation);
        }
    });

这个解决方案的问题在于 tvMessage.setText(toTooltip.getMessage());用于测量新高度的线立即展开 TextView 和 LinearLayout,然后动画将视图调整为之前的大小,然后再应用调整大小的动画,产生丑陋的视觉效果。

我相信 TextSwitcher 不会为高度变化设置动画,因此不是解决方案,但我没有尝试过。

【问题讨论】:

    标签: android animation textview


    【解决方案1】:

    我的问题的解决方案是使用虚拟 TextView 并使用新文本而不是使用实际的 TextView 来测量它。这样我的 TextView 不会在动画之前调整大小,我只需要在动画结束时设置文本。

    详情请看这个答案:Getting height of text view before rendering to layout

    【讨论】:

      【解决方案2】:

      您可以为此使用递归运行的处理程序,看起来也更干净一些。所以,例如,使用这个:

      调用函数startTextAnimation()

      在你的 onCreate() 中有一个Handler animHandler = new Handler();

      然后使用这个函数:

      public void startTextAnimation() {
         textView.setHeight(textView.getHeight() + 1);
         if (textView.getHeight < *whatheightyouwant*) {
             aninHandler.postDelayed(new Runnable() {
                 @Override
                 public void run() {
                     startTextAnimation();
                 }
             }, 200);
         }
      }
      

      您可以相应地设置参数,它应该可以完成这项工作。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2022-01-19
        • 2015-02-27
        • 1970-01-01
        • 2018-09-13
        • 1970-01-01
        • 2013-06-06
        • 2015-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多