Android Койтлин: перевод анимации на просмотр не работает - PullRequest
0 голосов
/ 11 апреля 2020

Я работаю над проектом Android Kotlin. Я применяю анимацию к представлениям. Исходя из основ, я пытаюсь анимировать вид изображения от нижней части экрана к центру экрана.

У меня есть макет XML со следующим кодом.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="@color/colorPrimaryDark"
    tools:context=".MainActivity">

    <LinearLayout
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/main_image_logo"
            android:src="@drawable/memento_text_logo"
            android:layout_width="@dimen/main_logo_image_width"
            android:layout_height="wrap_content" />
        <TextView
            android:textColor="@android:color/white"
            android:id="@+id/main_tv_slogan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/main_slogan"
            />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Я анимирую изображение lo go, переводящее снизу к центру (где оно изначально) в упражнении со следующим кодом.

private fun animateMainLogo() {
        val valueAnimator = ValueAnimator.ofFloat(0f, main_image_logo.y)

        valueAnimator.addUpdateListener {
            val value = it.animatedValue as Float
            main_image_logo.translationY = value
        }

        valueAnimator.interpolator = LinearInterpolator()
        valueAnimator.duration = 1000
        valueAnimator.start()
    }

Когда я запускаю код, это не оживляет вид. Это просто там, где это и есть c. Что не так с моим кодом и как я могу это исправить?

1 Ответ

1 голос
/ 11 апреля 2020

translationY вида в макете равно 0. Если вы хотите анимировать его снизу до текущей позиции - вам следует изменить translationY значения с некоторого положительного значения на 0.

private fun animateLogo() {
    val translationYFrom = 400f
    val translationYTo = 0f
    val valueAnimator = ValueAnimator.ofFloat(translationYFrom, translationYTo).apply {
        interpolator = LinearInterpolator()
        duration = 1000
    }
    valueAnimator.addUpdateListener {
        val value = it.animatedValue as Float
        main_image_logo?.translationY = value
    }
    valueAnimator.start()
}

То же самое можно сделать следующим образом:

private fun animateLogo() {
        main_image_logo.translationY = 400f
        main_image_logo.animate()
            .translationY(0f)
            .setInterpolator(LinearInterpolator())
            .setStartDelay(1000)
            .start()
    }

Добавьте эти строки в LinearLayout и ConstraintLayout, потому что без них LinearLayout будет вырезать части анимированного представления, когда оно выходит за пределы LinearLayout. 1013 *

android:clipChildren="false"
android:clipToPadding="false"

Или сделать main_image_logo прямым потомком root ConstraintLayout. Вот результат: result

...