You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现BottomNavigationView图标永久循环重复动画

实现BottomNavigationView图标无限循环动画的方案

方案1:纯XML实现(优先推荐)

  • 第一步:定义支持无限循环的矢量动画资源
    首先在res/animator目录下创建无限循环的属性动画,示例为图标呼吸缩放效果,你可以按需修改为平移、透明度变化、路径变形等其他动画属性:
    <!-- res/animator/loop_icon_anim.xml -->
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:propertyName="scaleY"
        android:duration="1200"
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        android:valueFrom="0.9"
        android:valueTo="1.1"
        android:valueType="floatType" />
    
  • 第二步:绑定动画到矢量图标
    在res/drawable下创建AnimatedVectorDrawable资源,将动画绑定到矢量图的对应目标节点:
    <!-- res/drawable/ic_loop_home.xml -->
    <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/ic_base_home"> <!-- 替换为你的基础矢量图标文件 -->
        <target
            android:name="icon_root" <!-- 对应基础矢量图里要做动画的节点name,必须完全匹配 -->
            android:animation="@animator/loop_icon_anim" />
    </animated-vector>
    
  • 第三步:给BottomNavigationView菜单项设置该图标
    在menu布局文件中,直接将目标item的android:icon属性设置为上面创建的ic_loop_home即可。

方案2:代码动态控制循环(兼容复杂场景)

如果你的动画逻辑需要动态调整,或者XML配置的无限动画未自动启动,可以通过代码强制维持循环:

// 取到目标菜单项
val targetItem = bottomNavigationView.menu.findItem(R.id.nav_your_item_id)
// 转成可动画的drawable类型
val animatedIcon = targetItem.icon as? Animatable2Compat ?: return
// 监听动画结束事件,结束立刻重启实现无限循环
animatedIcon.registerAnimationCallback(object : Animatable2Compat.AnimationCallback() {
    override fun onAnimationEnd(drawable: Drawable?) {
        super.onAnimationEnd(drawable)
        animatedIcon.start()
    }
})
// 页面可见时启动动画
animatedIcon.start()

注意事项

  • 页面进入后台、销毁时要主动调用animatedIcon.stop()并注销回调,避免内存泄漏
  • 如果需要给多个菜单项加循环动画,遍历menu所有item执行上述逻辑即可
  • 矢量图节点的name必须和animated-vector里target的name完全匹配才能生效

内容的提问来源于stack exchange,提问作者Sai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 08:45:06