【问题标题】:Animate the textview according to textview根据 textview 对 textview 进行动画处理
【发布时间】:2020-07-27 14:37:23
【问题描述】:

我有一个垂直线性布局,它有两个 textview,我在第一个 textview 中添加了 textsize 动画 所以问题是当文本视图更大并且每当在上部文本视图中添加新行时,下部文本视图只会向下移动而没有动画。它只是从以前的位置弹出并在新位置弹出。当第一个 textview 高度改变时,如何为第二个 textview 设置动画?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/one"
        android:text="one two three four five"
        android:textSize="@dimen/TEXT_SIZE_18"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/two"
        android:text="six seven eight nine ten"
        android:textSize="@dimen/TEXT_SIZE_18"/>

</LinearLayout>

我用动画一个文本视图大小

final float startSize = 18; // Size in pixels
final float endSize = 24;
long animationDuration = 600; // Animation duration in ms

ValueAnimator animator = ValueAnimator.ofFloat(startSize, endSize);
animator.setDuration(animationDuration);

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        float animatedValue = (float) valueAnimator.getAnimatedValue();
        if (one!=null){
            one.setTextSize(animatedValue);
        }
    }
});

我想让两个文本视图自动动画

【问题讨论】:

  • 添加带有问题的代码。
  • 我已添加代码请帮助@ADM
  • 我已经添加了一个答案,它在我的最后工作,看看是否适合你。

标签: android animation transition


【解决方案1】:

一个简单的方法是使用LayoutTransition。可以使用android:animateLayoutChanges="true" 属性在ViewGroup 上启用。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayout"
        android:animateLayoutChanges="true"
        android:orientation="vertical">
    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/one"
            android:text="one two three four five"
            android:textSize="@dimen/TEXT_SIZE_18"/>
    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/two"
            android:text="six seven eight nine ten"
            android:textSize="@dimen/TEXT_SIZE_18"/>
</LinearLayout>

然后您可以使用LayoutTransition.CHANGING启用转换以更改状态:-

  linearLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING)
    final float startSize = 20; // Size in pixels
    final float endSize = 60;
    long animationDuration = 10000; // Animation duration in ms
    ValueAnimator animator = ValueAnimator.ofFloat(startSize, endSize);
    animator.setDuration(animationDuration);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            float animatedValue = (float) valueAnimator.getAnimatedValue();
            if (one!=null){
                one.setTextSize(animatedValue);
            }
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-31
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多