`

【翻译】(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/

 

分享到:
评论

相关推荐

    Blend中文翻译教程

    - 纹理绘画:利用内置的画笔工具在模型上绘制纹理,实现色彩和细节的添加。 - 材质系统:学习如何配置不同的材质类型,如金属、玻璃、布料等,赋予模型真实感。 - UV映射:理解UV坐标的概念,以及如何正确展开和...

    c#课程设计

    4. **绘图板**:这个项目涉及图形用户界面(GUI)设计,使用C#的`Graphics`类和`Pen`, `Brush`对象绘制图形,允许用户在画布上自由绘画。理解坐标系统和图形变换也是必要的。 5. **俄罗斯方块**:这是一个游戏开发...

    Pygame官方手册(官方API)正版3美金下载的4000.zip

    9. 图形精灵与精灵组:`pygame.sprite`模块提供精灵类,用于管理游戏中可移动或交互的对象。精灵组(Group)则方便批量处理多个精灵,进行绘制和碰撞检测。 10. 图像变换:Pygame支持图像的缩放、旋转和翻转,通过`...

    Advanced Qt Programming

    书中的这部分内容可能会涵盖粒子系统、自定义绘画和高级动画效果的实现。 最后,本书可能还会涉及Qt的其他高级主题,如QML(Qt Meta Language),它是Qt的声明式UI语言,用于构建触摸友好的用户界面,以及如何与C++...

    PhotoShopCS2完美汉化补丁

    汉化补丁的工作原理通常是将软件原有的英文资源文件替换为翻译后的中文资源文件,包括菜单、对话框、提示信息等,确保软件的所有文字元素都能显示为中文。对于Photoshop CS2而言,这意味着用户可以更直观地找到并...

    Qt开发面试题.pdf

    - **QGraphicsScene/QGraphicsView:** 构建可缩放和可移动的图形视图。 - **QGraphicsItem:** 图形视图中的基础项。 5. **网络编程实现**: - **QTcpSocket/QHttp/QNetworkAccessManager:** 支持TCP/IP和...

    flutter_practices

    3. **动画**:Flutter提供了强大的动画库,可以创建平滑、复杂的过渡效果。 4. **测试**:Flutter支持单元测试、集成测试和Widget测试,确保代码质量。 5. **性能优化**:通过避免不必要的Widget重建、使用const...

    Qt 类层次结构图

    - **QtGui**:负责图形用户界面的构建,包括窗口系统集成、绘画、打印等功能。 - **QtNetwork**:提供网络通信支持。 - **QtOpenGL**:用于集成 OpenGL 绘制功能。 - **QtSql**:提供数据库访问接口。 - **QtSvg**:...

Global site tag (gtag.js) - Google Analytics