简体   繁体   English

Android 从右到左 textview 滑动 animation

[英]Android right to left textview sliding animation

I am currently building a clothing store app and one feature I want to implement is a kind of information banner along the top of the home fragment.我目前正在构建一个服装店应用程序,我想要实现的一个功能是沿着主页片段顶部的一种信息横幅。 I'm relatively new to using animations but I'm pretty much sure that it's the best option.我对使用动画比较陌生,但我很确定这是最好的选择。

I have a list of strings such as the following:我有一个字符串列表,如下所示:

val information = listOf("Information 1", "Information 2", "Information 3")

What I would like to do is have each string slide into the fragment from the right to the center (take 2 seconds), stay in the center for 3 seconds, then slide off screen to the left in 2 seconds whilst the next information is sliding in. An important point is I want this to repeat forever (as long as you stay on the Home fragment).我想做的是让每个字符串从右到中心滑入片段(需要 2 秒),在中心停留 3 秒,然后在 2 秒内滑出屏幕向左,同时下一个信息正在滑动重要的一点是我希望这会永远重复(只要你留在 Home 片段上)。

It would look something like this:它看起来像这样:

1) Left side of screen -> |                  Information 1                  | <- Right side of screen
2) Left side of screen -> | Information 1                     Information 2 | <- Right side of screen
3) Left side of screen -> |                  Information 2                  | <- Right side of screen

I expect to have to use two different animation files for this so I wrote the following:我希望为此必须使用两个不同的 animation 文件,所以我写了以下内容:

Right to center居中权

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:startOffset="3000">

    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0%p"
        android:duration="2000"/>
</set>

Center to left中心向左

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:startOffset="3000">

    <translate
        android:fromXDelta="0%p"
        android:toXDelta="-100%p"
        android:duration="2000"/>
</set>

I have no idea how to actually get this to work unfortunately and would really appreciate some assistance.不幸的是,我不知道如何真正让它工作,并且非常感谢一些帮助。

EDIT编辑

I have used view binding and a view model that stores the information as LiveData<List<String>> and the setting of the animations is done in the onCreateView function.我使用了视图绑定和视图 model,它将信息存储为LiveData<List<String>> ,并且动画的设置在onCreateView function 中完成。

The way I do it is like this:我这样做的方式是这样的:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    binding = FragmentHomeBinding.inflate(inflater, container, false)

    homeViewModel.setBannerInformation(listOf(
        "Information 1", 
        "Information 2", 
        "Information 3"
    ))

    homeViewModel.bannerInformation.observe(viewLifecycleOwner, { informationList ->
        val rightToCenter = AnimationUtils.loadAnimation(binding.bannerInformation.context, R.anim.banner_information_animation_right_to_center)
        val centerToLeft = AnimationUtils.loadAnimation(binding.bannerInformation.context, R.anim.banner_information_animation_center_to_left)

        binding.bannerInformation.text = informationList.first()
        binding.bannerInformation.startAnimation(rightToCenter)
        binding.bannerInformation.startAnimation(centerToLeft)
    })

    return binding.root
}

I have found a solution to my issue which seems like a hacky solution but so far it works.我找到了一个解决我的问题的方法,这似乎是一个 hacky 解决方案,但到目前为止它有效。

Firstly, create an animation resource file that does the animation you want首先,创建一个 animation 资源文件来执行您想要的 animation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0%p"
        android:duration="2000"/>

    <translate
        android:startOffset="3500"
        android:fromXDelta="0%p"
        android:toXDelta="-100%p"
        android:duration="2000"/>
</set>

Next, create a function that will define the animation and changes to occur接下来,创建一个 function 将定义 animation 并发生更改

private fun animateInformationBanner(information: List<String>, start: Int = 0) : Animation {
    var index = start
    val animation = AnimationUtils.loadAnimation(binding.bannerInformation.context, R.anim.banner_information_animation)
    animation.setAnimationListener(object: Animation.AnimationListener {
        override fun onAnimationStart(animation: Animation?) {
            binding.bannerInformation.text = information[index % information.size]
        }

        override fun onAnimationEnd(animation: Animation?) {
            index++
            binding.bannerInformation.text = null
            binding.bannerInformation.startAnimation(animation)
        }

        override fun onAnimationRepeat(animation: Animation?) {
            TODO("Not yet implemented")
        }
    })

    return animation
}

Finally, apply the animation to the TextView最后,将 animation 应用到 TextView

homeViewModel.bannerInformation.observe(viewLifecycleOwner, { informationList ->
    binding.bannerInformation.startAnimation(animateInformationBanner(informationList))
})

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

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