`

【翻译】(77)可绘画对象动画

 
阅读更多

【翻译】(77)可绘画对象动画

 

see

http://developer.android.com/guide/topics/graphics/drawable-animation.html

 

原文见

http://developer.android.com/guide/topics/graphics/drawable-animation.html

 

-------------------------------

 

Drawable Animation

 

可绘画对象动画

 

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资源以创建一个动画。从它是用一系列不同的图片创建,依次地播放,就像一卷胶卷(注:电影)的意义上看,这是一种传统动画。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>节点组成,每个子节点定义一个帧:一个用于该帧的可绘画对象资源和帧持续时间。这里有一个用于可绘画对象动画的示例XML文件:

 

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

    android:oneshot="true">

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

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

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

</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,那么动画将循环。使用这个在工程的res/drawable/目录中保存为rocket_thrust.xml的XML,它可以被添加到一个View作为背景图片,然后被调用以播放。这里有一个示例Activity,在它里面该动画被添加到一个ImageView然后在屏幕被触碰时被动画化。

 

AnimationDrawable rocketAnimation;

 

public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

 

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

  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);

  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

}

 

public boolean onTouchEvent(MotionEvent event) {

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

    rocketAnimation.start();

    return true;

  }

  return super.onTouchEvent(event);

}

 

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.

 

重要的是注意被调用在AnimationDrawable上的start()方法不能在你的Activity的onCreate()方法期间被调用,因为AnimationDrawable还没有被完全依附到窗口。如果你希望立即播放动画,不需要交互,那么你可能希望从你的Activity中的onWindowFocusChanged()方法里调用它,它(注:指onWindowFocusChanged())将在Android带你的窗口进入焦点时被调用。

 

For more information on the XML syntax, available tags and attributes, see Animation Resources.

 

想获得关于XML语法,可用的标签和属性的更多信息,参见动画资源。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 08 Mar 2012 0:34

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)

 

(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:

* ソフトウェア技術ドキュメントを勝手に翻訳

http://www.techdoctranslator.com/android

* Ley's Blog

http://leybreeze.com/blog/

* 农民伯伯

http://www.cnblogs.com/over140/

* Android中文翻译组

http://androidbox.sinaapp.com/

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics