2012-11-26 22 views
4

有沒有辦法將xmlanimation-list設置爲xml屬性?我可以設置和如下程序啓動它:如何將動畫列表設置爲xml屬性

ImageView img = (ImageView) findViewById(R.id.loadingImageView); 
    img.setBackgroundResource(R.drawable.loading_animation); 
    AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); 
    frameAnimation.start(); 

動畫列表是:

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

<item 
    android:drawable="@drawable/loading_anim_frame_one" 
    android:duration="50"/> 
<item 
    android:drawable="@drawable/loading_anim_frame_two" 
    android:duration="50"/> 

等等。

有沒有辦法做到這一點與xml標記只,即沒有Java代碼?

如果沒有,是否有辦法至少將其設置爲xml屬性,然後以編程方式啓動它?

我不能使用單個drawable的旋轉,因爲動畫是由幾個drawable按順序組成的。

+0

https://stackoverflow.com/questions/29603032/how-to-start-a-second-animation-list-有用的鏈接s = 0 | 2.9229 請幫我解決我的問題..我被卡住了很嚴重... – 2015-04-14 15:32:30

回答

16

你可以聲明它作爲

<set xmlns:android="http://schemas.android.com/apk/res/android" > 

    <scale 
     android:duration="700" 
     android:fillAfter="false" 
     android:fromXScale="0.0" 
     android:fromYScale="0.0" 
     android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toXScale="1.4" 
     android:toYScale="1.4" /> 

    <scale 
     android:duration="400" 
     android:fillBefore="false" 
     android:fromXScale="1.4" 
     android:fromYScale="1.4" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:startOffset="700" 
     android:toXScale="0.8" 
     android:toYScale="0.8" /> 

</set> 

只是一個參考。你將不得不改變動畫類型和參數。根據我的知識,你將不得不使用java啓動它。

編輯:

This是可以爲動畫列表

+0

謝謝。我必須使用動畫列表,因爲動畫由多個可繪製的按順序組成,而不是單個可繪製的動畫。我不確定在給定的情況下如何使用您的建議。任何建議表示讚賞。 –

+0

http://stackoverflow.com/questions/2136258/how-to-load-animationdrawable-from-xml-file。 Chck它。它可能有幫助 –

+0

該鏈接非常有幫助。如果你把它編輯成你的答案,我會接受它。 –

4

我覺得你應該先加載動畫:

Animation anim = AnimationUtils.loadAnimation(this, R.anim.animation_name); 

img.startAnimation(anim); 
+0

感謝您的回答。我的代碼加載它,並在Java中啓動工作正常。我正在尋找一種方法來重構它,所以至少我可以將動畫列表設置爲xml佈局中的圖像視圖。所以,這是我很好奇的xml部分。 –

相關問題