}
复制代码
封装个 BasicData,存放 App 内置的一些基本数据,这里主要针对 Lottie 文件:
val mNavigationAnimationList = arrayListOf(
LottieAnimation.HOME,
LottieAnimation.SUBSCRIBE,
LottieAnimation.DISCOVERY,
LottieAnimation.ACCOUNT
)
val mNavigationAnimationNightList = arrayListOf(
LottieAnimation.HOME_NIGHT,
LottieAnimation.SUBSCRIBE_NIGHT,
LottieAnimation.DISCOVERY_NIGHT,
LottieAnimation.ACCOUNT_NIGHT
)
复制代码
Step 3:导入对应依赖,新增 Lottie Utils
api ‘com.google.android.material:material:1.2.0’
api ‘com.airbnb.android:lottie:3.4.1’
复制代码
工具类方法:
/**
- 获取 Lottie Drawable
*/
fun getLottieDrawable(
animation: LottieAnimation,
bottomNavigationView: BottomNavigationView
): LottieDrawable {
return LottieDrawable().apply {
val result = LottieCompositionFactory.fromAssetSync(
bottomNavigationView.context.applicationContext, animation.value
)
callback = bottomNavigationView
composition = result.value
}
}
/**
- 获取不同模式下 Lottie json 文件
*/
fun getLottieAnimationList(context: Context): ArrayList {
return if (isDarkTheme(context)) {
mNavigationAnimationNightList
} else {
mNavigationAnimationList
}
}
复制代码
判断是否深色模式我单独提取了一个工具类,Lottie-android 中也有对深色模式的兼容方法:
/**
- 验证当前是否为深色模式
*/
fun isDarkTheme(context: Context): Boolean {
val flag = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return flag == Configuration.UI_MODE_NIGHT_YES
}
复制代码
Step 4:设置布局
先添加个 tab 字体选中和非选中的字体颜色 selecor:
<?xml version="1.0" encoding="utf-8"?> 复制代码
整一波布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background="@color/colorBackground"
tools:context=".module.home.activity.HomeActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_bottom_bar"
android:layout_width="