0

我已將BottomNavigationView添加到我的項目中,並且我想向其中一個菜單項添加動畫。這個動畫是一個AnimationDrawable,基本上是一系列的圖像。我已經完成了以下工作,但沒有工作,我怎麼能夠達到這個目標,或者嘗試一種替代方案?將AnimationDrawable添加到BottomNavigationView

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/lytMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:id="@+id/lytContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="?attr/actionBarSize" /> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/nav" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_alignParentBottom="true" 
     android:background="@color/white" 
     app:menu="@menu/menu" /> 

</RelativeLayout> 

menu.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/menShow" 
     android:enabled="true" 
     android:icon="@drawable/headphones" 
     android:title="@string/menu_listen" 
     app:showAsAction="ifRoom" /> 
    <item 
     android:id="@+id/menSearch" 
     android:enabled="true" 
     android:icon="@drawable/search" 
     android:title="@string/menu_search" 
     app:showAsAction="ifRoom" /> 

    <item 
     android:id="@+id/menPlay" 
     android:enabled="true" 
     android:icon="@drawable/play_anim" 
     android:title="@string/menu_play" 
     app:showAsAction="ifRoom" /> 
</menu> 

play_anim.xml:

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/play_anim" 
    android:oneshot="false"> 
    <item 
     android:drawable="@drawable/vis_f1" 
     android:duration="100" /> 
    <item 
     android:drawable="@drawable/vis_f2" 
     android:duration="100" /> 
    <item 
     android:drawable="@drawable/vis_f3" 
     android:duration="100" /> 
    <item 
     android:drawable="@drawable/vis_f4" 
     android:duration="100" /> 
    <item 
     android:drawable="@drawable/vis_f5" 
     android:duration="100" /> 
    <item 
     android:drawable="@drawable/vis_f6" 
     android:duration="100" /> 
    <item 
     android:drawable="@drawable/vis_f7" 
     android:duration="100" /> 
</animation-list> 

MainActivity.java:

BottomNavigationView bottomView = (BottomNavigationView) findViewById(R.id.nav); 

MenuItem menuItem = (MenuItem) bottomView.findViewById(R.id.menPlay); 

AnimationDrawable animation = (AnimationDrawable)menuItem.getIcon(); 

animation.start(); 

迄今爲止的結果是帶有3個靜態圖像的菜單,我只想爲它們中的一個製作動畫。

任何幫助將不勝感激,在此先感謝

回答

0

我會迴應自己。我發現使用的BottomNavigationBar第三方庫的解決方案:ButtomBar它很簡單,功能強大,但如何解決我的問題做以下幾點:

MainActivity.java:

private AppCompatImageView appCompatImageView; 
private AnimationDrawable animationDrawable; 
private BottomBar navBottom; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    navBottom = (BottomBar) findViewById(R.id.navBottom); 

    appCompatImageView = (AppCompatImageView) navBottom.getTabAtPosition(Constants.BOTTOM_BAR_ANIMATION).getChildAt(0); 
    animationDrawable = (AnimationDrawable) appCompatImageView.getDrawable(); 

    if(shouldStartAnimation()) 
    { 
     if(!animationDrawable.isRunning()) 
     { 
      animationDrawable.start(); 
     } 
    } 
    else 
    { 
     animationDrawable.stop(); 
    } 
} 

public boolean shouldStartAnimation() 
{ 
    //Add some condition in here 
    return true; 
} 

main_activity。 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/lytMain" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<FrameLayout 
    android:id="@+id/lytContent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginBottom="?attr/actionBarSize" /> 

<com.roughike.bottombar.BottomBar 
    android:id="@+id/navBottom" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:layout_alignParentBottom="true" 
    android:background="@color/black" 
    app:bb_activeTabColor="@drawable/sbs_menu_selector" 
    app:bb_behavior="iconsOnly" 
    app:bb_tabXmlResource="@xml/bottombar_items" /> 

我鑄造的菜單項的繪製到AppCompatImageView,然後拿到animationDrawa它只是簡單的appCompatImageView.getDrawable()就是這樣,那麼你可以開始/停止動畫繪製,因爲你需要它。

請注意,shouldStartAnimation是您的條件應放置的方法。

希望這可以幫助某人。

PS:謝謝roughike四個有用的庫。