Android逐帧动画(一)

本文介绍如何在Android中实现逐帧动画。通过创建AnimationDrawable对象并结合XML配置,可以轻松地制作出流畅的动画效果。文章提供了一个示例项目,包括布局文件、图片资源和动画控制逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     逐帧动画以很短的时间间隔连续显示一系列图像的过程。从编程角度讲:程序以固定时间逐个绘制一系列帧(图形)。

在Android中,使用AnimationDrawable完成逐帧动画的编码

    首先介绍AnimationDrawable一个用来创建逐帧动画的类,定义了一系列的Drawable对象,这些对象可用作view对象的背景。

    其次以一个例子介绍逐帧动画的最简单的实现方式:

    1、新建project,activity名为FrameAnimationActivity,相应布局文件为:activity_frame_animation.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"     android:gravity="center_horizontal"     tools:context=".FrameAnimationActivity" >

    <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/hello_world" />     <Button         android:id="@+id/startButton"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/startAnimation"/>     <ImageView         android:id="@+id/animationImage"         android:contentDescription="@string/app_name"         android:layout_width="wrap_content"         android:layout_height="wrap_content"/>

</LinearLayout>


 

其中,ImageView用于播放动画,Button用于控制动画开始和结束。

 

   2、准备若干动画中的图片

   3、在res/drawable/ 中创建一个xml文件,名为frame_animation.xml,文件用于定义动画:

 <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

    

<item android:drawable="@drawable/fish0" android:duration="100"/> <item android:drawable="@drawable/fish1" android:duration="100"/> <item android:drawable="@drawable/fish2" android:duration="100"/> <item android:drawable="@drawable/fish3" android:duration="100"/> <item android:drawable="@drawable/fish4" android:duration="100"/> <item android:drawable="@drawable/fish5" android:duration="100"/> <item android:drawable="@drawable/fish6" android:duration="100"/> <item android:drawable="@drawable/fish7" android:duration="100"/> <item android:drawable="@drawable/fish8" android:duration="100"/> <item android:drawable="@drawable/fish9" android:duration="100"/>

</animation-list>

其中 animation-list标记将对应于一个AnimationDrawable对象,每一个item指向一副图片,对应于动画中的一帧,属性duration为播放时间

    4、在FrameAnimationActivity编写代码:

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_frame_animation);
  inits();
 }

 private void inits() {
  strButton = (Button) findViewById(R.id.startButton);
  animationView = (ImageView) findViewById(R.id.animationImage);
  animationView.setBackgroundResource(R.drawable.fish0);
  strButton.setOnClickListener(new Button.OnClickListener() {

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    animationView.setBackgroundResource(R.drawable.frame_animation);
    AnimationDrawable frameAnimation = (AnimationDrawable) animationView
      .getBackground();
    if (frameAnimation.isRunning()) {
     frameAnimation.stop();
     strButton.setText(R.string.startAnimation);
    } else {
     frameAnimation.start();
     strButton.setText(R.string.endAnimation);
    }
   }

  });
 }

 

其中,关键代码:animationView.setBackgroundResource(R.drawable.frame_animation);

将xml实例化为Animation对象并设置为ImageView的背景 AnimationDrawable frameAnimation = (AnimationDrawable) animationView

获取通过xml文件定义并实例化后的Animation对象

frameAnimation.isRunning()判断动画是否正在运行

frameAnimation.start();开始动画

frameAnimation.stop();结束动画

最后,运行程序,观察逐帧效果。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值