【Android动画】之Frame动画
2013-12-23 18:53
302 查看
Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。
下面就讲一下Frame Animation。
其实使用起来比较简单,首先需要创建一个AnimationDrawable对象,通过addFrame方法把每一帧要显示的内容添加进去,最后通过Start方法来播放动画。 同时还有设置循环setOneShot等方法可供使用。
下面先看一下官方对AnimationDrawable类的说明:
An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call run() to start the animation.
An AnimationDrawable defined in XML consists of a single element, and a series of nested tags. Each item defines a frame of the animation. See the example below.
spin_animation.xml file in res/drawable/ folder:
Here is the code to load and play this animation.
相信看了这个说明,大家也都知道如何做了吧?
下面我们使用Frame动画做一个 loading 的效果。
首先准备素材,如下:
然后新建一个AnimationDrawable对象,把这些图片加载进去。 addFrame第一参数表示要加载的内容,第二参数表示持续时间。
代码:
AnimationDrawable 类是Drawable类的子类,因此可以将 AnimationDrawable 设为背景等来查看效果。
最后只要调用 frameAnimation.start(); 就可以启动该Frame动画了。
那么上面是使用Java代码的方式来加载的Frame动画, 也可以像Tween动画那样,使用纯XML局部文件来做。 API中已经有说明,这边不再赘述了。
直接看示例:
来源:http://my.eoe.cn/blue_rain/archive/651.html
下面就讲一下Frame Animation。
其实使用起来比较简单,首先需要创建一个AnimationDrawable对象,通过addFrame方法把每一帧要显示的内容添加进去,最后通过Start方法来播放动画。 同时还有设置循环setOneShot等方法可供使用。
下面先看一下官方对AnimationDrawable类的说明:
An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call run() to start the animation.
An AnimationDrawable defined in XML consists of a single element, and a series of nested tags. Each item defines a frame of the animation. See the example below.
spin_animation.xml file in res/drawable/ folder:
1 2 3 4 5 6 7 8 9 10 | <!-- Animation frames are wheel0.png -- wheel5.png files inside the res/drawable/ folder --> <animation-list android:id="selected" android:oneshot="false"> <item android:drawable="@drawable/wheel0" android:duration="50" /> <item android:drawable="@drawable/wheel1" android:duration="50" /> <item android:drawable="@drawable/wheel2" android:duration="50" /> <item android:drawable="@drawable/wheel3" android:duration="50" /> <item android:drawable="@drawable/wheel4" android:duration="50" /> <item android:drawable="@drawable/wheel5" android:duration="50" /> </animation-list> |
1 2 3 4 5 6 7 8 9 10 | // Load the ImageView that will host the animation and // set its background to our AnimationDrawable XML resource. ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image); img.setBackgroundResource(R.drawable.spin_animation); // Get the background, which has been compiled to an AnimationDrawable object. AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); // Start the animation (looped playback by default). frameAnimation.start() |
下面我们使用Frame动画做一个 loading 的效果。
首先准备素材,如下:
然后新建一个AnimationDrawable对象,把这些图片加载进去。 addFrame第一参数表示要加载的内容,第二参数表示持续时间。
代码:
1 2 3 4 5 6 7 8 | frameAnimation = new AnimationDrawable(); for (int i = 0; i < 10; i++) { int id = getResources().getIdentifier("load" + (i+1), "drawable", this.getContext().getPackageName()); frameAnimation.addFrame(getResources().getDrawable(id), 100); } //设置循环播放 false表示循环 true表示不循环,仅播放一次 frameAnimation.setOneShot(false); |
最后只要调用 frameAnimation.start(); 就可以启动该Frame动画了。
那么上面是使用Java代码的方式来加载的Frame动画, 也可以像Tween动画那样,使用纯XML局部文件来做。 API中已经有说明,这边不再赘述了。
直接看示例:
1 2 3 4 5 6 7 8 9 10 | <?xml version="1.0" encoding="utf-8"?> <animaltion-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/load1" android:duration="50" /> <item android:drawable="@drawable/load2" android:duration="50" /> <item android:drawable="@drawable/load3" android:duration="50" /> <item android:drawable="@drawable/load4" android:duration="50" /> <item android:drawable="@drawable/load5" android:duration="50" /> <item android:drawable="@drawable/load6" android:duration="50" /> <item android:drawable="@drawable/load7" android:duration="50" /> <item android:drawable="@drawable/load8" android:duration="50" /> <item android:drawable="@drawable/load9" android:duration="50" /> <item android:drawable="@drawable/load10" android:duration="50" /> </animaltion-list> |
来源:http://my.eoe.cn/blue_rain/archive/651.html
相关文章推荐
- Android动画(View Animation之Frame Animation(逐帧动画))
- Android:几种动画的使用(Tween动画、Frame动画、属性动画)
- Android动画效果之Frame Animation(逐帧动画)
- Android 动画之 ---- Drawable Animation (Frame动画,帧动画)
- Android两大动画之Frame(帧动画)详解
- Android笔记(六十三) android中的动画——逐帧动画( frame-by-frame animation)
- android之frame动画详解
- 赵雅智_android_frame动画
- android tween、frame动画
- Android开发之对Frame动画播放的补充
- android Frame动画---animation-list
- Android动画——Frame动画
- Android基础夯实--重温动画(二)之Frame Animation
- android Frame动画不能播放问题解析
- Android中的动画类型--Tween动画、还有一种是Frame动画
- Android动画Animation之Frame实现类似电影的动画效果
- 使用android frame动画定义自己的ProgressBar
- android之frame动画详解
- Android Frame动画--AnimationDrawable 和 SurfaceView
- android动画——Frame by Frame Animation