天天看點

【幀動畫總結】AnimationDrawable Frame

Drawable Animation 開發者文檔

位置: /sdk/docs/guide/topics/graphics/drawable-animation.html

Drawable animation lets you load a series of Drawable resources one after another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film. The AnimationDrawable class is the basis for Drawable animations. Drawable動畫可以讓您一個接一個地加載一系列的Drawable資源來建立動畫。 這是一種傳統的動畫,它的意義是建立一系列不同的圖像,按順序播放,像一卷電影。 AnimationDrawable類是可繪制動畫的基礎。

While you can define the frames of an animation in your code, using the AnimationDrawable class API, it's more simply accomplished with完成 a single XML file that lists the frames that compose the animation. The XML file for this kind of animation belongs in the res/drawable/ directory of your Android project. In this case, the instructions are the order and duration for each frame of the animation. 雖然您可以在代碼中定義動畫的架構,但使用AnimationDrawable類API,隻需完成一個列出構成動畫的架構的XML檔案即可完成。 這種動畫的XML檔案屬于您的Android項目的res / drawable /目錄。 在這種情況下,(需要設定的)指令是動畫每幀的順序和持續時間。

The XML file consists of an <animation-list> element as the root node and a series of child <item> nodes that each define a frame: a drawable resource for the frame and the frame duration. Here's an example XML file for a Drawable animation: XML檔案由作為根節點的<animation-list>元素和每個定義一個幀的一系列子<item>節點組成:每一幀的drawable資源和持續時間。 以下是可繪制動畫的XML檔案示例:

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

  2.     android:oneshot="true">

  3.     <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />

  4.     <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />

  5.     <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />

  6. </animation-list>

This animation runs for just three frames. By setting the android:oneshot attribute of the list to true, it will cycle just once then stop and hold on the last frame. If it is set false then the animation will loop. With this XML saved as rocket_thrust.xml in the res/drawable/ directory of the project, it can be added as the background image to a View and then called to play. Here's an example Activity, in which the animation is added to an ImageView and then animated when the screen is touched: 此動畫隻運作三幀。 通過将清單的android:oneshot屬性設定為true,它将循環一次,然後停止并按住最後一幀。 如果設定為false,則動畫将循環。 将此XML儲存為項目的res / drawable /目錄中的rocket_thrust.xml,可以将其作為背景圖像添加到View,然後調用播放。 下面是一個示例,其中将動畫添加到ImageView中,然後在觸摸螢幕時進行動畫:

  1. AnimationDrawable rocketAnimation;

  2. public void onCreate(Bundle savedInstanceState) {

  3. super.onCreate(savedInstanceState);

  4. setContentView(R.layout.main);

  5. ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);

  6. rocketImage.setBackgroundResource(R.drawable.rocket_thrust);

  7. rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

  8. }

  9. public boolean onTouchEvent(MotionEvent event) {

  10. if (event.getAction() == MotionEvent.ACTION_DOWN) {

  11. rocketAnimation.start();

  12. return true;

  13. }

  14. return super.onTouchEvent(event);

  15. }

It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus. 請注意,在Activity的onCreate()方法中,不能調用AnimationDrawable的start()方法,因為AnimationDrawable尚未完全附加到視窗。 如果要立即播放動畫,而不需要互動,那麼您可能希望在Activity中的onWindowFocusChanged()方法中調用它,當Android将您的視窗置于焦點時,該動畫将被調用。

AnimationDrawable 類

繼承體系

public class AnimationDrawable extends DrawableContainer implements Runnable, Animatable java.lang.Object    ↳ android.graphics.drawable. Drawable     ↳ android.graphics.drawable.DrawableContainer     ↳ android.graphics.drawable. AnimationDrawable 可以看到 AnimationDrawable 是 Drawable 的簡介子類。

公共方法

  • void addFrame(Drawable frame, int duration):Adds a frame to the animation
  • int getDuration(int i):Return the duration in milliseconds of the frame at the specified index
  • Drawable getFrame(int index):Return the Drawable at the specified frame index
  • int getNumberOfFrames():Return the number of frames in the animation
  • void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme):Inflate this Drawable from an XML resource optionally styled by a theme.
  • boolean isOneShot():Return true of the animation will play once, false otherwise
  • boolean isRunning():Indicates whether the animation is currently running or not.
  • Drawable mutate():Make this drawable mutable易變的.  This operation cannot be reversed反轉. 
  • void run():This method exists for implementation purpose only and should not be called directly.  Invoke start() instead.
  • void setOneShot(boolean oneShot):Sets whether the animation should play once or repeat.
  • boolean setVisible(boolean visible, boolean restart):Sets whether this AnimationDrawable is visible.
  • void start():Starts the animation, looping if necessary. This method has no effect if the animation is running.
  • void stop():Stops the animation. This method has no effect if the animation is not running.
  • void unscheduleSelf(Runnable what):Use the current Drawable.Callback implementation to have this Drawable unscheduled.

打底色的三個方法是 Animatable 接口中定義的方法

幀動畫簡介

Drawable Animation 或者 Frame Animation,幀動畫,就像GIF圖檔(或電影)一樣,是通過 依次顯示 一系列的 Drawable 來模拟動畫的效果。 以<animation-list>為根元素,一個<item>表示一幀要輪換顯示的圖檔 oneshot代表着是否隻展示一遍,設定為false會不停的循環播放動畫 duration屬性表示此幀顯示的時間

注意:幀動畫是指背景動畫,是以隻能設定 background 屬性為指定的幀動畫,或在代碼中通過 setBackgroundResource(R.drawable.amin_id) 指定 幀動畫 ,但是不能設定 src 屬性為 指定的幀動畫 。 當然兩者可以同時設定,此時src保持不變,背景是個動畫會改變。

注意:開啟或關閉 幀動畫 前最好先判斷擷取的 幀動畫 是否為空,因為 幀動畫 是通過 getBackground()  強 轉過來的,可能不存在!

最重要的:不要在 onCreate()中調用start方法開啟幀動畫,因為此時幀動畫還沒有完全跟Window相關聯,如果想要在 界 面顯示時就立即開始動畫的話,可以在onWindowFoucsChanged()方法中調用start方法。

雖然定義三種動畫的 xml檔案 都可以放置在res/anim/ 檔案夾 中,但是 建議 :

  • 幀動畫放在res/drawable檔案夾中
  • 補間動畫放在res/anim檔案夾中
  • 屬性動畫放在res/animator檔案夾中

null