帧动画Drawable Animation入门

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. TheAnimationDrawableclass is the basis for Drawable animations.

Drawable animation是一个一个的加载一些列的图片来创建动画的,他是一种传统的动画,事实上就是一个不同图片的序列AnimationDrawable是Drawable animations 的基类

While you can define the frames of an animation in your code, using theAnimationDrawableclass 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.

/目录下,事实上,所有的指令就是每一个帧动画的顺序和持续时间

The XML file consists of an 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 xmlns: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:oneshotattribute 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:

设置为true,那他就只会循环一次,,并停留在最后一个帧上,若为false的话,就会循环运行

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 theAnimationDrawable 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 method in your Activity, which will get called when Android brings your window into focus

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

如果困难是堵砖墙,拍拍它说你还不够高。

帧动画Drawable Animation入门

相关文章:

你感兴趣的文章:

标签云: