【发布时间】:2016-08-23 16:11:04
【问题描述】:
我在 LinearLayout 中有一个 TextView。我的目标是在 TextView 的文本发生变化并且需要更多或更少的行时为它的高度变化设置动画。
我是这样拆分工作的:
- 淡出旧文本
- 为 TextView 的新高度(和父级 LinearLayout)设置动画
- 淡入新文本
淡入淡出部分很简单,但我为高度变化动画而努力。
这是我的简化布局:
<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