Android动画-Drawable Animation
2016-05-23 13:19
393 查看
本章内容
在之前动画概述中,已经详细说了Drawable Animation,本章直接上效果。效果图:
代码中,我使用了两种方式来实现这种动画,先看xml:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/a" android:duration="150" /> <item android:drawable="@drawable/b" android:duration="150" /> <item android:drawable="@drawable/c" android:duration="150" /> <item android:drawable="@drawable/d" android:duration="150" /> <item android:drawable="@drawable/e" android:duration="150" /> <item android:drawable="@drawable/f" android:duration="150" /> <item android:drawable="@drawable/g" android:duration="150" /> <item android:drawable="@drawable/h" android:duration="150" /> <item android:drawable="@drawable/i" android:duration="150" /> </animation-list>
XML文件由作为根节点元素,在根节点里面,是一系列的的子节点,在子节点里面定义了要展示的图片和每一帧持续的时间,并且可以看到效果中,没有停止,这里在xml中我设置了 android:oneshot=”false”,这样就可以无限循环动画了,如果设置为 true,那么就只会进行一次动画,并停在最后一帧。
在ACTIVITY中的代码也很简单:
image.setBackgroundResource(R.drawable.drawable_list); drawable_animtaiton1 = (AnimationDrawable) image.getBackground(); .... drawable_animtaiton1.start();
第二种就是在代码动态添加图片和显示时间
drawable_animtaiton2 = new AnimationDrawable(); drawable_animtaiton2.addFrame(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.a)), DURATION2); drawable_animtaiton2.addFrame(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.b)), DURATION2); drawable_animtaiton2.addFrame(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.c)), DURATION2); drawable_animtaiton2.addFrame(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.d)), DURATION2); drawable_animtaiton2.addFrame(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.e)), DURATION2); drawable_animtaiton2.addFrame(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.f)), DURATION2); drawable_animtaiton2.addFrame(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.g)), DURATION2); drawable_animtaiton2.addFrame(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.h)), DURATION2); drawable_animtaiton2.addFrame(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.i)), DURATION2); image2.setBackground((AnimationDrawable)drawable_animtaiton2); ..... drawable_animtaiton2.start();
需要注意的是:AnimationDrawable的start()方法不能够在Activity的onCreate()调用,因为这个时候,AnimationDrawable可能还没有完全的绑定到Window上。如果我们想马上播放动画,我们可以在 onWindowFocusChanged()方法里面调用,因为这个方法是在window已经获取到焦点之后回调的,可以保证已经绑定结束。
源码地址:
http://download.csdn.net/detail/cjh_android/9531161
相关文章推荐
- 解决谷歌为Android系统提供的支持库android-support-v4。jar不能绑定查看源码的问题
- Android studio 配置出炫酷logcat颜色
- 高德地图-AndroidStadio-No implementation found for long com.autonavi.amap.mapcore.MapCore.nativeNewInsta
- Android Studio代码着色插件
- Android同步类:Mutex和Condition的使用方式
- Vungle Android和iOS SDK高级设置——方法和监听含义和对比
- Android 手机拍照适配攻略
- Android 学习笔记之十一 2048的实现分析
- IDA 调试android 遇到的问题及解决方案
- Android实现带有记住密码功能的登陆界面
- 关于在android中,如何一步到位,全局替换控件样式的一些看法
- Android屏幕自适应研究
- 学习Android SQLite 的介绍和使用(二)
- android studio 查找sha1
- Android动画
- Android设计模式之建造者模式——AlertDialog
- DrawerLayout
- android SQLite基本操作
- Android studio配置干活
- Retrofit2 更新指南