帧动画Drawable Animation入门_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:44:01
原创
1265人浏览过

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.

 虽然可以使用AnimationDrawable类在代码中定义帧动画,但最好是使用XML文件列出组成动画的所有帧。这个xml文件放在 res/drawable/目录下,事实上,所有的指令就是每一个帧动画的顺序和持续时间

立即学习前端免费学习笔记(深入)”;

The XML file consists of an element as the root node and a series of child 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="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:

上面的例子是一个3帧动画, 通过将android:oneshot设置为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 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

注意:start()方法不能在你的Activity的onCreate()方法中调用,因为那时候AnimationDrawable尚未完全附着于窗口。如果你必须立刻调用动画,切没有交互的话,可以在onWindowFocusChanged()中调用。因为这个方法是窗口获得焦点时调用的


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

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号