9.Android学习之动画与多媒体(一)

目录

9.动画与多媒体(一)

1.Android中的动画

1-1.实现逐帧动画

1-2.实现补间动画


9.动画与多媒体(一)

1.Android中的动画

在应用Android进行项目开发时,特别是在游戏开发时,经常需要涉及动画。Android中的动画通常可以分为逐帧动画和补间动画两种。

1-1.实现逐帧动画

逐帧(Frame)动画 就是顺序播放提前准备好的静态图像,利用人眼的“视觉暂留”原理,给用户造成动画的错觉。实现逐帧动画比较简单,只需要以下两个步骤:

(1)在Android的XML资源文件中定义一组用于生成动画的图片资源,可以包含一系列<item></item>子标记的<animation-list></animation-list>标记来实现,具体语法格式如下:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
                android:oneshot="true/false">
    <item android:drawable="@drawable/图片资源名1" android:duration="integer"/>
    ···<!-- 省略了部分<item></item>标记 -->
    <item android:drawable="@drawable/图片资源名n" android:duration="integer"/>
</animation-list>

在上面的语法中,android:oneshot 属性用于设置是否循环播放,默认值为true,表示循环放;android:drawable属性用于指定要显示的图片资源;android:duration 属性用于指定图片资源持续的时间。

(2)使用步骤(1) 中定义的动画资源。通常情况下,可以将其作为组件的背景使用。例如,可以在布局文件中添加一个线性布局管理器,然后将该布局管理器的android:background 属性设置为所定义的动画资源。也可以将定义的动画资源作为ImageView的背景使用。

注:在Android中还支持在Java代码中创建逐帧动画。具体的步骤是:首先创建AnimationDrawable对象,然后调用addFrame()方法向动画中添加帧,每调用一次addFrame()方法,将添加一个帧。

例:

fairy.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/img001" android:duration="60"/>
    <item android:drawable="@drawable/img002" android:duration="60"/>
    <item android:drawable="@drawable/img003" android:duration="60"/>
    <item android:drawable="@drawable/img004" android:duration="60"/>
    <item android:drawable="@drawable/img005" android:duration="60"/>
    <item android:drawable="@drawable/img006" android:duration="60"/>
</animation-list>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:id="@+id/main_ll1"
    android:background="@drawable/fairy">
</LinearLayout>

MainActivity.java

package com.example.frame_by_frameanimation;
​
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
​
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
​
public class MainActivity extends AppCompatActiv
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值