Android中Frame的播放介绍

首先,在res下创建anim文件夹,建立dance.xml,duration表示这个图片的播放时间,单位毫秒,的代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"> <!-- true表示不循环 -->

<item android:drawable="@drawable/p1" android:duration="500" />
<item android:drawable="@drawable/p0" android:duration="500" />
<item android:drawable="@drawable/p2" android:duration="600" />

</animation-list>

然后,在layout下创建dances.xml,添加如下代码

<!-- Frame动画图片 -->

    <ImageView
        android:id="@+id/ImgDance"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

创建类Animation_,如下:

// 定义AnimationDrawable
private AnimationDrawable mAnimationDrawable = null;
private ImageView img;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dances);

  // 定义一个动画布局文件
  img = (ImageView) findViewById(R.id.ImgDance);

  // 装载动画布局文件(也可以写在xml中,即 背景图片)
  img.setBackgroundResource(R.anim.dance);
  // 构建动画
  mAnimationDrawable = (AnimationDrawable) img.getBackground();
  // 是否循环
  // mAnimationDrawable.setOneShot(false);
  // 设置该类显示的动画
  img.setBackgroundDrawable(mAnimationDrawable);
}

/*
  * 不能在Activity的onCreate()方法里调用start方法,因为此时AnimationDrawable类尚未完全与window接触,
  * 可以安排一个TouchEvent触发启动animation
  * ,如果希望一开始就播放动画,就要加入一个onWindowFocusChanged()方法来启动,
  */

public void onWindowFocusChanged(boolean hasFocus) {
  // TODO Auto-generated method stub
  super.onWindowFocusChanged(hasFocus);
  if (hasFocus) {
   mAnimationDrawable.start();
  } else {
   mAnimationDrawable.stop();
  }
}

如果该动画页面还要跳转,要 等待动画结束再开始跳转
,可以添加线程,也可以用Timer事件,后面的时间就设置动画放多长的时间

  Timer timer = new Timer();
  TimerTask tast = new TimerTask() {
   public void run() {
    Intent intent = new Intent(Animation_.this, MainActivity.class);
    startActivity(intent);
    finish();
   }
  };
  timer.schedule(tast, 1600);

更多请关注我的新浪微博博客地址:http://blog.sina.com.cn/u/2804695260

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值