Android用Animation-list实现逐帧动画

先看看效果图

下面是2个动画的xml文件

animation1.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!--
 根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画        
 根标签下,通过item标签对动画中的每一个图片进行声明        android:duration 表示展示所用的该图片的时间长度
-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <item
        android:drawable="@drawable/icon1"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon2"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon3"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon4"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon5"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon6"
        android:duration="250">
    </item>

</animation-list>
复制代码

animation2.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!--
 根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画        
 根标签下,通过item标签对动画中的每一个图片进行声明        android:duration 表示展示所用的该图片的时间长度
-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <item
        android:drawable="@drawable/icon6"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon5"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon4"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon3"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon2"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon1"
        android:duration="250">
    </item>

</animation-list>
复制代码

xml布局文件:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/animationIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:src="@anim/animation1" 
        android:scaleType="center"/>

    <Button
        android:id="@+id/buttonA"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:onClick="onClick"
        android:text="顺序显示" />

    <Button
        android:id="@+id/buttonB"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:onClick="onClick"
        android:text="停止" />

    <Button
        android:id="@+id/buttonC"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:onClick="onClick"
        android:text="倒序显示" />

</LinearLayout>
复制代码

java代码:

复制代码
package com.example.animationdemo;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private ImageView animationIV;
    private AnimationDrawable animationDrawable;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        animationIV = (ImageView) findViewById(R.id.animationIV);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.buttonA:
            start_animation(R.anim.animation1);
            break;
        case R.id.buttonB:
            stop_animation();
            break;
        case R.id.buttonC:
            start_animation(R.anim.animation2);
            break;
        default:
            break;
        }
    }
    private void start_animation(int id){
        animationIV.setImageResource(id);
        animationDrawable = (AnimationDrawable) animationIV
                .getDrawable();
        animationDrawable.start();
    }
    private void stop_animation(){
        animationDrawable.stop();
    }
}
复制代码

 csdn下载地址:http://download.csdn.net/detail/wenwei19861106/4856995

 

posted on   南瓜饼  阅读(3444)  评论(4编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2012年12月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示