Android动画之帧动画(一)
2015-11-25 14:36
435 查看
定义
在时间轴的每帧上逐帧绘制不同的内容,使其连续播放而成动画。优点:适合表演细腻复杂的动画
缺点:给制作增加了工作量,且文件输出很大。
XML实现方式
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
start()to
run the animation.
创建帧动画最简单的方式是在XML文件中定义animation,文件放在res/drawable/文件夹下(放在自定义的anim中也可以)。将它作为view的背景,并通过start()方法来运行animation。
定义aniamtion文件example_animation.xml文件,在<animation-list></animation-list>元素中定义动画的全部帧,并制定各帧的持续时间。
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <!-- 定义一个动画帧,Drawable为img0,持续时间50毫秒 --> <item android:drawable="@drawable/img0" android:duration="50" /> </animation-list>android:onshot的属性值为true时表示只播放一次,false表示循环播放。
XML中background没有设置动画
<pre name="code" class="html">ImageView view= (ImageView) findViewById(R.id.view); // 通过逐帧动画的资源文件获得AnimationDrawable示例 AnimationDrawable=(AnimationDrawable) getResources().getDrawable(R.drawable.example_animation);
// 把AnimationDrawable设置为ImageView的背景view.setBackgroundDrawable(<span style="font-family: Roboto, sans-serif;">AnimationDrawable</span><span style="font-family: Roboto, sans-serif;">);</span>
XML中background设置了动画
final AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground(); animationDrawable.start();
运行动画
if (AnimationDrawable!= null && !AnimationDrawable.isRunning()) { frameAnim.start(); Toast.makeText(ToXMLActivity.this, "开始播放", 0).show(); }
使用Java代码实现
通过animationDrawable.addFrame()的方式添加帧动画。imageView view = (ImageView) findViewById(R.id.example_aniamtion); AnimationDrawable animationDrawable=new AnimationDrawable(); // 为AnimationDrawable添加动画帧 animationDrawable.addFrame(getResources().getDrawable(R.drawable.img0), 50); // 设置ImageView的背景 AnimationDrawableview.setBackgroundDrawable(animationDrawable);
参考链接:http://www.cnblogs.com/plokmju/p/android_AnimationDrawable.html
相关文章推荐
- 使用android:process的注意事项
- 【Android】Activity与Fragment的生命周期的关系
- Android学习----Activity
- Android 开机启动服务
- android破解和重新打包
- 使用GifView在android上显示gif图片
- Android重点知识(三)事件分发机制
- Android性能优化之常见的内存泄漏
- 从零学Android(十二)、Android中的图形和动画之属性动画
- uri is not registered
- Android共享文件的几种方式
- Android Studio中获取SHA1
- Android四大基本组件介绍与生命周期
- Android之手机出现"已安装了存在签名冲突的同名数据包"的原因及解决办法
- Android监听键盘显示和隐藏
- Android获取RAM大小,内部存储大小,外部存储大小
- Android之toolbar的使用
- IntentService简介
- Android 加载不同 DPI 资源与内存消耗间的关系
- Android代码生成TextView,Button,CheckBox等控件的设置技巧