0

私はBottomNavigationViewをプロジェクトに追加しました。メニュー項目の1つにアニメーションを追加したいと思います。このアニメーションは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つの静止画像を持つメニューです。そのうちの1つをアニメーション化したいだけです。

すべてのヘルプは、事前

答えて

0

に私は自分自身に応えますおかげで理解されるであろう。それはかなりシンプルかつパワフルだButtomBarが、私は私の問題をやっている解決方法を次に示します:私はBottomNavigationBarのためのサードパーティのライブラリを使用して解決策を見つけた

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 4つの便利なライブラリ。

関連する問題