2013-05-09 56 views
3

我有一些動畫圖像(幀),如IMAGE_1,IMAGE_2和image_3.I的希望在我的應用程序中使用這個動畫形象,所以我嘗試如何在android中製作隨機幀動畫?

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false" > 

    <item 
     android:drawable="@drawable/image_1" 
     android:duration="2000"/> 
    <item 
     android:drawable="@drawable/image_2" 
     android:duration="50"/> 
    <item 
     android:drawable="@drawable/image_3" 
     android:duration="50"/> 

</animation-list> 

並將此作爲我的佈局的背景

public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    if(hasFocus){ 

     final AnimationDrawable bgDrawable = (AnimationDrawable) ((LinearLayout) findViewById(R.id.animationLayout)) 
       .getBackground(); 
     ((LinearLayout) findViewById(R.id.animationLayout)) 
       .post(new Runnable() { 

        @Override 
        public void run() { 
         bgDrawable .start(); 
        } 
       }); 

     } 
    } 
} 

但是這顯示動畫每次以相同的順序重複。我想通過使用這些圖像來製作random frame animation。我該如何做到這一點?

由於提前

回答

3

您可以創建一個新的AnimationDrawable,而是結合了XML到它,只是手動創建幀並將它們添加到AnimationDrawable

AnimationDrawable animationDrawable = new AnimationDrawable(); 

animationDrawable.addFrame("You BitmapDrawable for first frame", 2000); 
animationDrawable.addFrame("You BitmapDrawable for second frame", 50); 
animationDrawable.addFrame("You BitmapDrawable for third frame", 50); 

有一個很好的教程在那裏:

更新:

你可以有一個功能復位AnimationDrawable,並在特定的時間隨機填充它。這僅僅是一個僞代碼,以便對其進行調整您的需求,用正確的語法:要洗牌幀每次

public AnimationDrawable shuffleFrame(AnimationDrawable ad) { 

    ad = null; 
    ad = new AnimationDrawable(); 

    // Create your Drawable frames here 
    ... 

    // For each Drawable, pick a random one here and add the frame 
    ... 
    ad.addFrame(Randomly picked frame, duration); 


    return ad; 
} 

調用此函數。

+0

但是這顯示圖像幀在每次相同的順序 – 2013-05-09 09:48:44

+0

@DevuSoman,你得開玩笑吧。只要改變線條的順序即可。 '1。第二幀「,」2。第三幀「,」3。第一幀「 – 2013-05-09 09:51:12

+0

我問你如何通過改變幀的順序在特定的時間間隔後重復動畫 – 2013-05-09 09:57:59