第三十讲:Android之Animation(五)

本文介绍了一种基于Android平台实现逐帧动画的方法。通过创建FrameAnimation并利用不同状态的小狐狸图标,实现了动画效果。布局文件定义了ImageView显示动画,并提供按钮控制动画的开始与停止。

天行健,君子以自强不息。——《周易·乾·象》


本讲内容:逐帧动画 Frame Animation

逐帧动画 Frame Animation就是说一帧一帧的连起来播放就变成了动画,和放电影的机制非常相似。

我们通过一个样例感受一下,代码的解说都写在凝视里了

以下是res/layout/activity_main.xml 布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.text.MainActivity$PlaceholderFragment" >
 <ImageView 
        android:id="@+id/frame_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <Button 
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="runFrame"/>
    <Button 
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="stopFrame"/>
</LinearLayout>

以下是新建的res/anim/frame.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="@drawable/b1" android:duration="300" />  
    <item android:drawable="@drawable/b2" android:duration="300" />  
    <item android:drawable="@drawable/b3" android:duration="300" />  
    <item android:drawable="@drawable/b4" android:duration="300" />  
</animation-list> 

b1、b2、b3、 b4是四张不同小狐狸图标


以下是MainActivity.java主界面文件:

public class MainActivity extends Activity implements OnClickListener {
	private Button start;
	private Button stop;
	private ImageView iv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		iv = (ImageView) findViewById(R.id.frame_image);
		start = (Button) findViewById(R.id.start);
		stop = (Button) findViewById(R.id.stop);
		start.setOnClickListener(this);
		stop.setOnClickListener(this);
		
		iv.setBackgroundResource(R.anim.frame);
	}
	@Override
	public void onClick(View v) {
		AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
		switch (v.getId()) {
		case R.id.start:
			// 调用动画可画对象的開始播放方法
			anim.start();
			break;
		case R.id.stop:
			// 调用动画可画对象的停止播放方法
			anim.stop();
			break;
		}
	}
}

以下是执行结果:



本说到这里,谢谢大家!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值