Animation动画效果之Frame动画

  Frame动画 其实就是逐帧动画,用法也比Tween动画简单,只需要创建一个AnimationDrawable对象来表示Frame动画,然后通过addFrame方法把每一帧要显示的内容加进去就行了,最后通过start方法就可以播放这个动画了,通过还可以使用   setOneShot()方法 来设置动画是否重复播放。

       再这里,还需要设置图片的所在位置,首先要在res/anim目录下创建一个xml配置文件,用于存放图片资源的索引,配置的是一个以<animation-list>根原素和<item>子元素。

下面我们可以使用xml布局文件来实现Frame动画。首先在xml文件通过oneshot设置动画是否重复播放,然后加所有的帧都列出来。如下代码:

<?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/a1" android:duration="800"/>
    <item android:drawable="@drawable/a2" android:duration="800"/>
    <item android:drawable="@drawable/a3" android:duration="800"/>
    <item android:drawable="@drawable/a4" android:duration="800"/>
    <item android:drawable="@drawable/a5" android:duration="800"/>
    <item android:drawable="@drawable/a6" android:duration="800"/>
    <item android:drawable="@drawable/a7" android:duration="800"/>
    <item android:drawable="@drawable/a8" android:duration="800"/>
    <item android:drawable="@drawable/a9" android:duration="800"/>
    <item android:drawable="@drawable/a10" android:duration="800"/>
    <item android:drawable="@drawable/a11" android:duration="800"/>
    <item android:drawable="@drawable/a12" android:duration="800"/>
    <item android:drawable="@drawable/a13" android:duration="800"/>
    <item android:drawable="@drawable/a14" android:duration="800"/>
    <item android:drawable="@drawable/a15" android:duration="800"/>

</animation-list>

最后在程序中通过setBackgroundResource方法加载这个XML动画布局文件,通过getBackground方法得到动画,通过setBackgroundDrawable方法设置要显示的动画,通过start方法启动播放。

package com.hanfeng.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;

public class GameView extends View {
	/*定义AnimationDrawable动画对象*/
	private AnimationDrawable frameanim = null;
	Context context = null;

	public GameView(Context context) {
		super(context);
		this.context = context;
		/*定义一个ImageView用来显示动画*/
		ImageView img = new ImageView(context);
		/*状态布局文件*/
		img.setBackgroundResource(R.drawable.frame_animation);
		/*构建动画*/
		frameanim = (AnimationDrawable)img.getBackground();
		/*设置是否循环*/
		frameanim.setOneShot(false);
		/*设置该类显示的动画*/
		this.setBackgroundDrawable(frameanim);
		/*启动播放*/
		frameanim.start();
	}
	
}

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      gameView = new GameView(this);
      setContentView(gameView);
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值