Drawable Animation

来源:互联网 发布:海康威视无网络视频 编辑:程序博客网 时间:2024/05/18 20:50

中文名就是逐帧动画,顾名思义,一帧一帧的播放。那么写代码肯定是把一堆图片,按顺序排列,让组件一帧一帧的切换图片。

先上一个最基本的在xml里写的drawable animation

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/test_anim1" android:duration="200" />
    <item android:drawable="@drawable/test_anim2" android:duration="200" />
    <item android:drawable="@drawable/test_anim3" android:duration="200" />
    <item android:drawable="@drawable/test_anim4" android:duration="200" />
</animation-list>

 

drawable下面建立一个animation-list

Android:drawable 设置动画显示的图片

Android:duration  设置动画显示的时间

android:oneshot="true" 只显示一次

 

下面是一个简单的layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id = "@+id/drawable_anim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

</FrameLayout>

 

 

package com.yuanxzh.anim;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.yuanxzh.R;


public class TestDrawableAnimation extends Activity {
    private ImageView drawableAnim;
    private AnimationDrawable loadingAnimation;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_drawable_anim);
        drawableAnim = (ImageView)findViewById(R.id.drawable_anim);
        drawableAnim.setBackgroundResource(R.drawable.test_layerlist_anim);
        loadingAnimation = (AnimationDrawable) drawableAnim.getBackground();
    }
}

 

 

也可以在动画的xml里加上

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:duration="500">
        <layer-list>
            <item android:drawable="@drawable/test_anim1" />
            <item android:drawable="@drawable/bol_blue" />
        </layer-list>
    </item>
    <item android:duration="500">
        <layer-list>
            <item android:drawable="@drawable/test_anim2" />
            <item android:drawable="@drawable/bol_blue" />
        </layer-list>
    </item>
    <item android:duration="500">
        <layer-list>
            <item android:drawable="@drawable/test_anim3" />
            <item android:drawable="@drawable/bol_blue" />
        </layer-list>
    </item>
    <item android:duration="500">
        <layer-list>
            <item android:drawable="@drawable/test_anim4" />
            <item android:drawable="@drawable/bol_blue" />
        </layer-list>
    </item>
    <item android:duration="500">
        <layer-list>
            <item android:drawable="@drawable/test_anim5" />
            <item android:drawable="@drawable/bol_blue" />
        </layer-list>
    </item>
</animation-list>

 

 

layer-list 两张图片重叠在一块

 

 

 

原创粉丝点击