[英]Android right to left textview sliding animation
我目前正在构建一个服装店应用程序,我想要实现的一个功能是沿着主页片段顶部的一种信息横幅。 我对使用动画比较陌生,但我很确定这是最好的选择。
我有一个字符串列表,如下所示:
val information = listOf("Information 1", "Information 2", "Information 3")
我想做的是让每个字符串从右到中心滑入片段(需要 2 秒),在中心停留 3 秒,然后在 2 秒内滑出屏幕向左,同时下一个信息正在滑动重要的一点是我希望这会永远重复(只要你留在 Home 片段上)。
它看起来像这样:
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
我希望为此必须使用两个不同的 animation 文件,所以我写了以下内容:
居中权
<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>
中心向左
<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>
不幸的是,我不知道如何真正让它工作,并且非常感谢一些帮助。
编辑
我使用了视图绑定和视图 model,它将信息存储为LiveData<List<String>>
,并且动画的设置在onCreateView
function 中完成。
我这样做的方式是这样的:
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
}
我找到了一个解决我的问题的方法,这似乎是一个 hacky 解决方案,但到目前为止它有效。
首先,创建一个 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>
接下来,创建一个 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
}
最后,将 animation 应用到 TextView
homeViewModel.bannerInformation.observe(viewLifecycleOwner, { informationList ->
binding.bannerInformation.startAnimation(animateInformationBanner(informationList))
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.