Держите элемент ограничения высоты по центру, используя макеты движения - PullRequest
0 голосов
/ 15 января 2020

Задача

Я пытаюсь анимировать смахивание заголовка, показывающего некоторые TextView с фиксированной высотой и центрированное изображение, которое должно соответствовать остальной высоте.

При смахивании TextViews должны двигаться вверх и исчезать, что прекрасно работает с анимацией alpha. Изображение должно уменьшаться и затухать при смахивании, но оставаться в его центрированном положении поверх TextViews.

Проблема

Но пока изображение имеет android:layout_height="0dp" и является ограничением между верхом заголовка и верхней части TextViews, он анимируется в верхнем левом углу, как показано в примере видео ниже.

Если высота (например, android:layout_height="200dp") задается изображению, оно остается в центре во время анимация.

Я воссоздал проблему с помощью простого примера, приведенного ниже.

Мои вопросы, таким образом:

  • Как сохранить изображение в центре, если высота Dynami c?
  • Или это ошибка в библиотеке Android?

Пример приложения

activity_main. xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context=".MainActivity"
    tools:showPaths="true"
    app:layoutDescription="@xml/scene">

    <View
        android:id="@+id/headerBg"
        android:background="#000"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/headerRemainder"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:background="#ff0000"
        app:layout_constraintTop_toTopOf="@id/headerBg"
        app:layout_constraintBottom_toTopOf="@id/headerBottom"
        app:layout_constraintStart_toStartOf="@id/headerBg"
        app:layout_constraintEnd_toEndOf="@id/headerBg"
        />

    <TextView
        android:id="@+id/headerBottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00ff00"
        android:text="Header Bottom"
        app:layout_constraintBottom_toBottomOf="@id/headerBg"
        app:layout_constraintStart_toStartOf="@id/headerBg"
        app:layout_constraintEnd_toEndOf="@id/headerBg"
        />

    <TextView
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#999"
        android:text="Hello World!"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/headerBg" />

</androidx.constraintlayout.motion.widget.MotionLayout>

сцена. xml

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000">
        <OnSwipe
            motion:dragDirection="dragUp"
            motion:touchAnchorId="@id/headerBg"
            motion:touchAnchorSide="bottom" />

        <ConstraintSet android:id="@+id/start">

            <Constraint
                android:id="@id/headerBg"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

            <Constraint
                android:id="@id/headerRemainder"
                android:layout_width="100dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="@id/headerBg"
                app:layout_constraintBottom_toTopOf="@id/headerBottom"
                app:layout_constraintStart_toStartOf="@id/headerBg"
                app:layout_constraintEnd_toEndOf="@id/headerBg"
                />

        </ConstraintSet>

        <ConstraintSet android:id="@+id/end">

            <Constraint
                android:id="@id/headerBg"
                android:layout_height="0dp"
                android:layout_width="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

            <Constraint
                android:id="@id/headerRemainder"
                android:layout_width="100dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="@id/headerBg"
                app:layout_constraintBottom_toTopOf="@id/headerBottom"
                app:layout_constraintStart_toStartOf="@id/headerBg"
                app:layout_constraintEnd_toEndOf="@id/headerBg"
                />

        </ConstraintSet>
    </Transition>
</MotionScene>

Пример видео

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...