android - 如何使用 ObjectAnimator 为 BottomNavigationView 菜单项图标设置动画
问题描述
我正在使用 android 支持设计BottomNavigationView
进行底部标签导航。
<android.support.design.widget.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="match_parent"
android:layout_height="56dp"
app:labelVisibilityMode="unlabeled"
app:itemIconSize="40dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
app:itemBackground="@color/blue_active"
app:menu="@menu/nav_items">
</android.support.design.widget.BottomNavigationView>
我想做的是:
ObjectAnimator
按下时以编程方式为选项卡(菜单)图标设置动画
这是菜单:
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="@string/nav_home" />
<item
android:id="@+id/nav_games"
android:icon="@drawable/games"
android:title="@string/nav_games" />
<item
android:id="@+id/nav_profile"
android:icon="@drawable/profile"
android:title="@string/nav_profile" />
</menu>
代码:
mMainNav.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_home -> {
//item.icon is drawable
var myAnimation = ObjectAnimator.ofFloat(item.icon,"rotation",0f,360f)
myAnimation.duration = 500
myAnimation.start() //nothing happens
setFragment(HomeFragment)
true
}
有了这个没有任何动画。
怎么了 ?我应该使用其他方式制作动画还是我只是应用错误?
我尝试使用可绘制图标为图像视图设置动画,然后将其设置为项目操作视图,但这也不起作用。(会发生一些反应,但会产生一些不相关的奇怪行为)
var drawable = applicationContext.getDrawable(R.drawable.ic_home)
var someImageView = ImageView(this)
someImageView.setImageDrawable(drawable)
var myAnimation = ObjectAnimator.ofFloat(someImageView,"rotation",0f,100f)
myAnimation.duration = 2000
myAnimation.start()
item.actionView = someImageView
赏金链接已损坏,请检查: https ://streamable.com/99pa8
解决方案
尝试以下解决方案
步骤1
使用ShapeShifter创建动画矢量 drawable并将此动画矢量 drawable 从 Android Studio 导入您的 Android 项目,并将其放入.src/res/drawable
第2步
为底部导航视图中的每个图标创建一个动画状态列表 Drawable。
anim_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:targetApi="16">
<!-- provide a different drawable for each state-->
<item android:id="@+id/state_on"
android:drawable="@drawable/ic_settings_active_avd"
android:state_checked="true"/>
<item android:id="@+id/state_off"
android:drawable="@drawable/ic_settings_inactive"/>
<!-- specify transitions -->
<transition
android:drawable="@drawable/ic_settings_active_avd"
android:fromId="@id/state_off"
android:toId="@id/state_on" />
</animated-selector>
步骤 - 3
使用这个可绘制的动画状态列表作为图标。
menu_bottom_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item_settings_fragment"
android:icon="@drawable/anim_settings"
android:title="@string/settings"
app:showAsAction="always" />
...
</menu>
第4步
将此menu_bottom_nav.xml设置为底部导航视图的菜单。
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:showAsAction="always|withText"
app:itemTextColor="@drawable/bottom_navigation_tab_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/menu_bottom_nav"
android:background="@color/colorWhite"
app:elevation="0dp"/>
现在在您的设备上运行 android 应用程序并检查底部导航栏动画。
有关更多详细信息,请首选此链接。
我希望这可以帮助你!
推荐阅读
- c - 如何为 32 位 iOS 编译 Vim8
- angular - 摩纳哥编辑器 - onDidBlurEditorText 与 onDidBlurEditorWidget
- apache-spark - Spark 中 DAG 优化规则
- ruby-on-rails - 为什么 ActionCable 的“异步”适配器不应该在生产中使用?
- python - 如何找到给定数据集的成对不相交集?
- asp.net-core - ProfileDataRequestContext.RequestedClaimTypes 中缺少子声明
- eclipse - EGit - 无法获得退出代码
- sql - 如何编写 SQL 查询以获取存储过程的所有修改日期
- android - Chrome 徽章显示在已安装 PWA 的“添加到主屏幕”上(仅安装快捷方式)
- flutter - Firestore 交易中的 Flutter 应用程序崩溃