Android帧动画(Drawable Animation)的使用总结
2016-06-24 17:53
351 查看
前言:
帧动画是由一系列的图片一张跟着一张播放形成动画效果;AnimationDrawable类是帧动画里边的基础;
虽然你可以在代码中通过AnimationDrawable来定义动画的每一帧,但是在XML中列出动画的每一帧会
更简单一些,这个XML文件应该放在res/drawable/目录下;在这种情况下,指令是由动画每一帧的顺序
和持续时间所组成的;这个XML文件由一个<animation-list>根节点和一系列定义每一帧的<item>子节点所组成;
以下是一个XML帧动画的示例:
这个帧动画的运行只有三帧图片所组成;通过设置android:oneshot为true,那么,它就会仅仅执行一遍,
然后停留到最后一帧上;如果设置android:oneshot为false,那么它就会一直循环,在本示例中,这个XML
文件在这个工程的res/drawable/目录下命名为了rocket_thrust.xml,所以它可以作为一个View控件的背景
图片,然后调用它去播放;以下是一个关于Acitivity的示例,这个动画被添加到了ImageView上,然后当
点击屏幕时,则形成动画效果;
值得注意一点的是:这个start()方法不能在Activity的onCreate()方法里边去调用,因为AnimationDrawable并不是完全依附于window的;
示例如下:
1>将每一帧动画放入 res/drawable目录下(复制粘贴即可),如:
2>在drawable目录下新建一个XML文件,在Android Studio中,其具体步骤如下:
选中res -----> 右键单击new ----> Android resource file ,如:
3>修改前三项(File name / Resource type / Root element),如示例:
4>在生成的bird.xml中添加<item>,bird.xml的最终结果为:
5>编写示例代码如下:
MainActivity:
activity_main.xml:
bird.xml:
运行结果如下:
总结:
帧动画主要是通过一帧一帧动画的播放来达到目的的;
源码:
帧动画(Drawable Animation)示例源码
帧动画是由一系列的图片一张跟着一张播放形成动画效果;AnimationDrawable类是帧动画里边的基础;
虽然你可以在代码中通过AnimationDrawable来定义动画的每一帧,但是在XML中列出动画的每一帧会
更简单一些,这个XML文件应该放在res/drawable/目录下;在这种情况下,指令是由动画每一帧的顺序
和持续时间所组成的;这个XML文件由一个<animation-list>根节点和一系列定义每一帧的<item>子节点所组成;
以下是一个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>
这个帧动画的运行只有三帧图片所组成;通过设置android:oneshot为true,那么,它就会仅仅执行一遍,
然后停留到最后一帧上;如果设置android:oneshot为false,那么它就会一直循环,在本示例中,这个XML
文件在这个工程的res/drawable/目录下命名为了rocket_thrust.xml,所以它可以作为一个View控件的背景
图片,然后调用它去播放;以下是一个关于Acitivity的示例,这个动画被添加到了ImageView上,然后当
点击屏幕时,则形成动画效果;
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); }
值得注意一点的是:这个start()方法不能在Activity的onCreate()方法里边去调用,因为AnimationDrawable并不是完全依附于window的;
示例如下:
1>将每一帧动画放入 res/drawable目录下(复制粘贴即可),如:
2>在drawable目录下新建一个XML文件,在Android Studio中,其具体步骤如下:
选中res -----> 右键单击new ----> Android resource file ,如:
3>修改前三项(File name / Resource type / Root element),如示例:
4>在生成的bird.xml中添加<item>,bird.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/bird1" android:duration="200" /> <item android:drawable="@drawable/bird2" android:duration="200" /> <item android:drawable="@drawable/bird3" android:duration="200" /> <item android:drawable="@drawable/bird4" android:duration="200" /> <item android:drawable="@drawable/bird5" android:duration="200" /> <item android:drawable="@drawable/bird6" android:duration="200" /> <item android:drawable="@drawable/bird7" android:duration="200" /> <item android:drawable="@drawable/bird8" android:duration="200" /> <item android:drawable="@drawable/bird9" android:duration="200" /> </animation-list>
5>编写示例代码如下:
MainActivity:
package com.example.administrator.testdrawableanimation; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private AnimationDrawable birdAnimation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * 获取ImageView控件 * **/ ImageView bird = (ImageView) findViewById(R.id.bird); /** * 设置ImageView控件的背景 * **/ bird.setBackgroundResource(R.drawable.bird); /*** * 得到AnimationDrawable对象 * **/ birdAnimation = (AnimationDrawable) bird.getBackground(); } /*** * 切记: * AnimationDrawable.start()方法不能放在onCreate()里边; * **/ public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { /*** * 开启动画 * **/ birdAnimation.start(); return true; } return super.onTouchEvent(event); } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.administrator.testdrawableanimation.MainActivity"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bird" /> </RelativeLayout>
bird.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/bird1" android:duration="200" /> <item android:drawable="@drawable/bird2" android:duration="200" /> <item android:drawable="@drawable/bird3" android:duration="200" /> <item android:drawable="@drawable/bird4" android:duration="200" /> <item android:drawable="@drawable/bird5" android:duration="200" /> <item android:drawable="@drawable/bird6" android:duration="200" /> <item android:drawable="@drawable/bird7" android:duration="200" /> <item android:drawable="@drawable/bird8" android:duration="200" /> <item android:drawable="@drawable/bird9" android:duration="200" /> </animation-list>
运行结果如下:
总结:
帧动画主要是通过一帧一帧动画的播放来达到目的的;
源码:
帧动画(Drawable Animation)示例源码
相关文章推荐
- Android常用的设计模式(三)
- Android onResume() 返回Activity时调用
- 详细配置android studio的gradle
- 自定义View之绘制钟表
- Android5.0中Cardview的简单使用
- 在ListView的item项里使用CheckBox或者Button时,OnItemClickListener无响应的问题
- Android系统启动流程分析之启动应用
- 改变imageButton按下和抬起时的background图片
- Android Studio快捷键和使用技巧
- Android Material Design 兼容库的使用
- Android中的广播机制(二)----- 发送广播
- Android 图片选择器
- 解决ubuntu eclipse中 Android SDK Manager 图标不见的方法
- Android keycode列表
- Android AlertDialog对话框用法示例
- Android高效加载大图、多图解决方案,有效避免程序OOM
- android studio降低编译版本出错
- 【Android】关于Activity的生命周期及一些理解
- android 调用支付宝充值,提示系统繁忙,4000
- android PixelFormat与ImageFormat的对比