Анимировать текстовое представление согласно textview - PullRequest
0 голосов
/ 14 апреля 2020

У меня вертикальный линейный макет с двумя текстовыми представлениями. Я добавил текстовую анимацию к первому текстовому представлению. Таким образом, проблема в том, что когда текстовое представление больше и когда новая строка добавляется в верхний текстовый вид, нижний текстовый вид просто перемещается вниз без анимации. , Он просто всплывает из предыдущей позиции и падает в новой позиции. Как я могу анимировать второе текстовое представление для анимирования при изменении высоты первого текстового представления ??

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

Я хочу, чтобы два текстовых просмотра анимировали автоматически

1 Ответ

0 голосов
/ 15 апреля 2020

Простой способ использования LayoutTransition. который можно включить на ViewGroup с помощью атрибута android:animateLayoutChanges="true".

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