如何使用垂直翻译在一个文本视图中重复替换文本?

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

我正在尝试为一个文本视图交替使用三种不同的文本。我想使用垂直翻译,其中文本从下到上显示并不断重复此动画。使用我当前的代码,我一直遇到一个问题,即文本视图一直垂直翻译到父布局顶部。我只希望动画发生在 textView 的高度内。任何建议将不胜感激。感谢您的宝贵时间。

我创建了一个自定义动画。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%p"
        android:toYDelta="-100%p"
        android:duration="3000"
        android:repeatCount="0"
        android:fillAfter="true"
        />
</set>

val linearLayout = findViewById(R.id.linearLayout)
val textView = findViewById(R.id.textView)

val animation = AnimationUtils.loadAnimation(this, R.anim.ex1)

animation.setAnimationListener(object : Animation.AnimationListener {
    override fun onAnimationStart(animation: Animation) {}

    override fun onAnimationEnd(animation: Animation) {
        if (textView.text == "First Text") {
            textView.text = "Second Text"
        } else {
            textView.text = "First Text"
        }
        linearLayout.startAnimation(animation)
    }

    override fun onAnimationRepeat(animation: Animation) {}
})

linearLayout.startAnimation(animation)
android user-interface animation android-animation
1个回答
0
投票

我认为这是因为你在 LinearLayout 上应用了动画。相反,在 TextView 上应用动画。

textView.startAnimation(animation)

另外,如果您分享 xml 布局,我们可以更好地理解问题。

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