Android用Animation-list实现逐帧动画

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值