Android Animation——drawable animation

来源:互联网 发布:微信开源mars源码分析 编辑:程序博客网 时间:2024/05/18 02:02

概述

目前动画分为以下三种:

  • Drawable Animation
  • View Animation
  • Property Animation

Drawable Animation 在一些书籍中也叫逐帧动画。顾名思义,这种动画是由一张张图片的有序播放构成的,有点类似于git动图。Drawable Animation 是三种动画中最容易实现的,因为对于开发来说,我们只需要保证图片有序播放即可。

项目实践

简要说明下Drawable Animation制作的主要步骤:

  • 在res/drawable/ 目录下,利用图片序列创建新的drawable资源
  • 将新的drawable资源在ImageView上显示
  • 通过ImageView将drawable资源强转为AnimationDrawable对象
  • 调用AnimationDrawable对象的start()和stop()方法实现动画的启动和停止

在res/drawable目录构建新的drawable资源

具体代码如下:

<?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/wifi_0" android:duration="200"/>    <item android:drawable="@drawable/wifi_1" android:duration="200"/>    <item android:drawable="@drawable/wifi_2" android:duration="200"/>    <item android:drawable="@drawable/wifi_3" android:duration="200"/>    <item android:drawable="@drawable/wifi_4" android:duration="200"/>    <item android:drawable="@drawable/wifi_5" android:duration="200"/></animation-list>

android:oneshot为true表示动画只执行一次,并停留在最后一帧;为false表示动画会一直循环执行。
item标签表示动画中的一帧,而且item的顺序就是动画中每帧动画执行的顺序。
android:drawable表示的是图片资源
android:duration表示这一帧显示的时间,单位为ms。

通过ImageView获得AnimationDrawable对象

创建布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1">        <ImageView            android:id="@+id/iv_wifi"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:layout_alignParentLeft="true"            android:layout_centerVertical="true"/>    </RelativeLayout>>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Start"        android:onClick="startWifiAnimation"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Stop"        android:onClick="stopWifiAnimation"/></LinearLayout>

获取AnimationDrawable对象,并通过button来控制动画的开始与停止

public class MainActivity extends AppCompatActivity {    private ImageView mWifiImage;    private AnimationDrawable mWifiAnimation;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mWifiImage = (ImageView) findViewById(R.id.iv_wifi);        mWifiImage.setBackgroundResource(R.drawable.icon_wifi);        mWifiAnimation = (AnimationDrawable)mWifiImage.getBackground();    }    @Override    public void onWindowFocusChanged(boolean hasFocus) {        super.onWindowFocusChanged(hasFocus);        mWifiAnimation.start();    }    public void startWifiAnimation(View view){        if(mWifiAnimation != null){            mWifiAnimation.start();        }    }    public void stopWifiAnimation(View view){        if(mWifiAnimation != null){            mWifiAnimation.stop();        }    }}

如果想要在启动Activity的时候就启动动画,不要在onCreate中直接调用animation的start()方法,因为此时AnimationDrawable还未真正加载到Activity中。应该在onWindowFocusChanged中调用animation的start()方法。
然而,我自己在搭载Android7.0的华为荣耀8手机上,直接在onCreate中调用animation的start()方法,结果显示Activity启动的时候animation能够正常播放。

相关项目下载: https://github.com/zhanghuiever/DrawableAnimation

原创粉丝点击