欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android逐帧动画实现代码

程序员文章站 2023-12-21 08:09:04
逐帧动画(frame-by-frame animations)顾名思义就是一帧接着一帧的播放图片,就像放电影一样。可以通过xml实现也可以通过java代码实现。逐帧动画适合...

逐帧动画(frame-by-frame animations)顾名思义就是一帧接着一帧的播放图片,就像放电影一样。可以通过xml实现也可以通过java代码实现。逐帧动画适合实现比较简单的动画效果,如果要实现复杂动画不太建议使用逐帧动画。

xml方式实现:

step 1 : 在res/drawable目录下一个文件sample_animlist.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="@mipmap/sample_1"
  android:duration="100" />
 <item
  android:drawable="@mipmap/sample_2"
  android:duration="100" />
 <item
  android:drawable="@mipmap/sample_3"
  android:duration="100" />
 <item
  android:drawable="@mipmap/sample_4"
  android:duration="100" />
 <item
  android:drawable="@mipmap/sample_5"
  android:duration="100" />
 <item
  android:drawable="@mipmap/sample_6"
  android:duration="100" />
</animation-list>

动画列表(animation-list)由一个或者多个item节点组成,item节点用来声明一个动画帧
drawable: 该帧的图片资源
duration : 播放时间 单位为毫秒
oneshot : 是否只播放一次,true表示只会播放一次,false表示一直循环播放

step 2:

利用imageview作为播放载体

 <imageview
 android:id="@+id/iv_ani"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@drawable/sample_animlist" />


step3: 在activity实现初始化 、暂停、播放。

public class mainactivity extends appcompatactivity {

 private imageview iv_ani;
 private animationdrawable manimationdrawable;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  iv_ani = (imageview) findviewbyid(r.id.iv_ani);
  manimationdrawable = (animationdrawable) iv_ani.getdrawable();
 }

 @override
 public void onwindowfocuschanged(boolean hasfocus) {
  super.onwindowfocuschanged(hasfocus);
  //window初始化完毕 开始播放动画
  animationstart();
 }

 @override
 protected void onpause() {
  animationstop();
  super.onpause();
 }

 private void animationstart() {
  if (null != manimationdrawable)
   manimationdrawable.start();//开始播放
 }

 private void animationstop() {
  if (null != manimationdrawable)
   manimationdrawable.stop();//暂停播放
 }
}

纯java方式实现

public class mainactivity extends appcompatactivity {

 private imageview iv_ani;
 private animationdrawable manimationdrawable;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  iv_ani = (imageview) findviewbyid(r.id.iv_ani);
  initanimationdrawable();
 }

 private void initanimationdrawable(){
  manimationdrawable = new animationdrawable();
  for (int i = 1; i <= 4; i++) {
   int id = getresources().getidentifier("sample_" + i, "mipmap", getpackagename());
   drawable drawable = getresources().getdrawable(id);
   manimationdrawable.addframe(drawable, 100);
  }
  manimationdrawable.setoneshot(false);
  iv_ani.setimagedrawable(manimationdrawable);

 }
}

addframe(drawable frame, int duration) : 添加一帧,并设置该帧显示的持续时间

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: