Android Kotlin。视图中的翻译动画不起作用

问题描述 投票:0回答:1

我正在做一个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>

我在活动中用下面的代码将标志图像从底部转换到中心(原来的位置)的动画。

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()
    }

当我运行这段代码时,它没有对视图进行动画。它只是在原地静止不动。我的代码有什么问题,如何解决?

android animation android-animation
1个回答
1
投票

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()
    }

把这行加到 LinearLayoutConstraintLayout 因为没有他们 LinearLayout 当动画视图的部分内容不在其范围内时,将被剪切。LinearLayout 界。

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

或使 main_image_logo 嫡系子孙 ConstraintLayout. 这是结果。result

© www.soinside.com 2019 - 2024. All rights reserved.