您的位置:首页 > 移动开发 > Android开发

Android用Animation-list实现逐帧动画

2014-11-15 14:33 369 查看


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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: