简体   繁体   English

Animation 问题 Android BottomNavigationView with Lottie

[英]Animation problem Android BottomNavigationView with Lottie

I'm trying to use BottomNavigationView with the lib Lottie in order to make icon animations.我正在尝试将 BottomNavigationView 与 lib Lottie 一起使用以制作图标动画。 I'd rather use Lottie instead VectorAndroidAnimation because I want more complex animations.我宁愿使用 Lottie 而不是 VectorAndroidAnimation,因为我想要更复杂的动画。 However the first item of BottomNavigationView does not animate and the other items do when I click on them但是, BottomNavigationView 的第一项没有动画,当我单击它们时,其他项目没有动画

(Gif below) (下面的动图)

三个项目

Below is my code:下面是我的代码:

 class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)

        navView.menu.apply {
            add(Menu.NONE, 0, Menu.NONE, R.string.title_home).icon = getLottieDrawable(
                LottieAnimation.HOME,
                navView
            )
            add(Menu.NONE, 1, Menu.NONE, R.string.title_dashboard).icon = getLottieDrawable(
                LottieAnimation.CALENDAR,
                navView
            )
            add(Menu.NONE, 2, Menu.NONE, R.string.title_notifications).icon = getLottieDrawable(
                LottieAnimation.BELL,
                navView
            )
        }

        navView.setOnNavigationItemSelectedListener(this)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        val icon = item.icon as? LottieDrawable
        icon?.apply {
            playAnimation()
        }
        return true
    }

    private fun getLottieDrawable(
        animation: LottieAnimation,
        view: BottomNavigationView
    ): LottieDrawable {
        return LottieDrawable().apply {
            val result = LottieCompositionFactory.fromAssetSync(
                view.context.applicationContext, animation.value
            )
            callback = view
            composition = result.value
        }
    }

}

enum class LottieAnimation(val value: String) {
    HOME("home.json"),
    CALENDAR("calendar.json"),
    BELL("bell.json")
} 

Tested with just two items into BottomNavigationView and the first item of tab was animate仅在 BottomNavigationView 中测试了两个项目,并且选项卡的第一个项目是动画的

(Two Items Gif below) (下面的两个项目 Gif)

有两个项目

And finally I tested with five items in the BottomNavigationView, and in this test the first and second item didn't animated when clicked, just the last three items on the tab最后我在 BottomNavigationView 中测试了五个项目,在这个测试中,第一个和第二个项目在点击时没有动画,只有选项卡上的最后三个项目

有五个项目

How about using a custom bottom navigation view.如何使用自定义底部导航视图。 You can add a Lottie view in each tab which starts animation on click.您可以在每个选项卡中添加一个 Lottie 视图,单击时从 animation 开始。 In my experience, a custom bottom navigation view has worked better for me than using Android's Bottom Navigation View.根据我的经验,自定义底部导航视图比使用 Android 的底部导航视图更适合我。

I faced the same issue and it was a nightmare.. but I finally found the solution!我遇到了同样的问题,这是一场噩梦......但我终于找到了解决方案!

You have to set the icons of your menu items AFTER adding them all .您必须在全部添加后设置菜单项的图标。 Do a first loop to add your items and then a second one to set the icon of each brand new menu item.执行第一个循环以添加您的项目,然后执行第二个循环以设置每个全新菜单项的图标。

            ...
            // First you add them ALL
            add(Menu.NONE, 0, Menu.NONE, R.string.title_home)
            add(Menu.NONE, 1, Menu.NONE, R.string.title_dashboard)
            add(Menu.NONE, 2, Menu.NONE, R.string.title_notifications)

            // Then you update their icons
            findItem(0).icon = getLottieDrawable(
                LottieAnimation.HOME,
                navView
            )
            findItem(1).icon = getLottieDrawable(
                LottieAnimation.CALENDAR,
                navView
            )
            findItem(2).icon = getLottieDrawable(
                LottieAnimation.BELL,
                navView
            )
            ...

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

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