las*_*ny4 7 android android-animation
我正在使用 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>
Run Code Online (Sandbox Code Playgroud)
我想做的是:
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>
Run Code Online (Sandbox Code Playgroud)
代码:
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
}
Run Code Online (Sandbox Code Playgroud)
有了这个,没有任何动画。
怎么了 ?我应该使用另一种方式来制作动画还是我只是应用错误?
我尝试使用 icon drawable 为 imageview 设置动画,然后将其设置为 items actionview 但这也不起作用。(发生了一些反应,但产生了一些无关的奇怪行为)
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
Run Code Online (Sandbox Code Playgroud)
赏金链接已损坏,请检查:https : //streamable.com/99pa8
尝试以下解决方案
第1步
使用ShapeShifter创建动画矢量可绘制对象并将此动画矢量可绘制对象从 Android Studio 导入您的 Android 项目并将其放置在.src/res/drawable
第2步
为底部导航视图中的每个图标创建一个动画状态列表 Drawable。
动画设置.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>
Run Code Online (Sandbox Code Playgroud)
步骤 - 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>
Run Code Online (Sandbox Code Playgroud)
第四步
设置此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"/>
Run Code Online (Sandbox Code Playgroud)
现在在您的设备上运行 android 应用程序并检查底部导航栏动画。
有关更多详细信息,请选择此链接。
我希望这可以帮助你!