简体   繁体   English

Android Kotlin:翻译 animation 视图不起作用

[英]Android Kotlin: Translate animation on view is not working

I am working on an Android Kotlin project.我正在研究 Android Kotlin 项目。 I am applying animation on views.我在视图上应用 animation。 Starting from the basics, I am trying to animate an image view from the bottom of the screen to the center of the screen.从基础开始,我试图将图像视图从屏幕底部动画到屏幕中心。

I have an XML layout with the following code.我有一个带有以下代码的 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>

I am animating the logo image translating from the bottom to the center (where it is originally) in the activity with the following code.我正在使用以下代码为活动中从底部转换到中心(它原来的位置)的徽标图像设置动画。

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

When I run the code, it is not animating the view.当我运行代码时,它不会为视图设置动画。 It is just there where it is and static.它就在它所在的位置和 static。 What is wrong with my code and how can I fix it?我的代码有什么问题,我该如何解决?

translationY of view in layout is 0. If you want to animate it from bottom to current position - you should change translationY values from some positive value to 0.布局中视图的translationY为0。如果要将其从底部动画到当前position - 您应该将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()
}

Same thing can be done this way:同样的事情可以这样做:

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

Add this lines to LinearLayout and ConstraintLayout because without them LinearLayout will cut of parts of animated view when it is outside of LinearLayout bounds.将此行添加到LinearLayoutConstraintLayout ,因为没有它们, LinearLayout将在动画视图超出LinearLayout边界时剪切部分动画视图。

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

Or make main_image_logo direct child of root ConstraintLayout .或者使main_image_logo成为根ConstraintLayout的直接子级。 Here is result:这是结果: 结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM