【动画系列】Drawable动画

来源:互联网 发布:数据使用安全保密协议 编辑:程序博客网 时间:2024/04/30 07:14

一.说明:

我们依旧可以使用xml或者java方式实现帧动画。但是依旧推荐使用xml,具体如下:
animation-list必须是根节点,包含一个或者多个元素,属性有:

android:oneshot true代表只执行一次,false循环执行。
item类似一帧的动画资源。
item animation-list的子项,包含属性如下:

android:drawable 一个frame的Drawable资源。
android:duration 一个frame显示多长时间。

二.原理

帧动画的原理很简单:就像老式电影胶卷那样,快速掠过一些列的图片,“帧”其实就是一张图片,因此创建一个自定义帧动画的第一步就是建立图片序列。
我们有两种选择:使用xml的drawable(比如shape drawable)或者是使用实际的图片。

三.例子

见图:

这里写图片描述

1.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="@mipmap/market_loading_01"        android:duration="100"        />    <item        android:drawable="@mipmap/market_loading_02"        android:duration="100"        />    <item        android:drawable="@mipmap/market_loading_03"        android:duration="100"        />    <item        android:drawable="@mipmap/market_loading_04"        android:duration="100"        />    <item        android:drawable="@mipmap/market_loading_05"        android:duration="100"        />    <item        android:drawable="@mipmap/market_loading_06"        android:duration="100"        />    <item        android:drawable="@mipmap/market_loading_07"        android:duration="100"        />    <item        android:drawable="@mipmap/market_loading_08"        android:duration="100"        />    <item        android:drawable="@mipmap/market_loading_09"        android:duration="100"        />    <item        android:drawable="@mipmap/market_loading_10"        android:duration="100"        /></animation-list>
        // frame动画        final ImageView imageView = (ImageView) findViewById(R.id.btn3111);        imageView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                imageView.setBackgroundResource(R.drawable.drawable_progress);                AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();                animationDrawable.start();            }        });

2.代码实现

AnimationDrawable animationDrawable2 = new AnimationDrawable();  Drawable drawable = getResources().getDrawable(R.drawable.fengjing_1);  Drawable drawable2 = getResources().getDrawable(R.drawable.fengjing_2);  Drawable drawable3 = getResources().getDrawable(R.drawable.fengjing_3);      animationDrawable2.addFrame(drawable, 1000);      animationDrawable2.addFrame(drawable2, 1000);      animationDrawable2.addFrame(drawable3, 1000);      animationDrawable2.setOneShot(false);      imageView.setBackgroundDrawable(animationDrawable2);      animationDrawable2.start();  

3.下载地址

https://github.com/zhujainxipan/AndroidStudy

四.总结

  1. Frame动画也可在Java代码或者xml中写,但是提倡大家还是在xml中写。
  2. Frame动画在xml中的根节点是animation-list其中的oneshot=false是循环播放,为true的话则播放到最后一张图片就会停止播放。
  3. 因为Frame 动画是有一堆静态图构成的所以,可以当成background。
  4. 特别注意,AnimationDrawable的start()方法不能在Activity的onCreate方法中调运,因为AnimationDrawable还未完全附着到window上,所以最好的调运时机是onWindowFocusChanged()方法中。
0 0