android简单的逐帧动画

android简单的逐帧动画

逐帧动画定义

逐帧动画是一种常见的动画形式(Frame By Frame),其原理是在“连续的关键帧”中分解动画动作,也就是在时间轴的每帧上逐帧绘制不同的内容,使其连续播放而成动画。 因为逐帧动画的帧序列内容不一样,不但给制作增加了负担而且最终输出的文件量也很大,但它的优势也很明显:逐帧动画具有非常大的灵活性,几乎可以表现任何想表现的内容,而它类似与电影的播放模式,很适合于表演细腻的动画。

简单说来,就是我们发的动态gif表情例如:

在这里插入图片描述

制作逐帧动画

我找了这张图片

在这里插入图片描述

找图片剪裁图片

用画板剪裁并且使用一定的命名规律命名(img_00.png):

在res目录的drawable目录下放置这些图片:

在这里插入图片描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述 在这里插入图片描述在这里插入图片描述 在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述 在这里插入图片描述在这里插入图片描述 在这里插入图片描述在这里插入图片描述

帧动画配置文件

在res目录的drawable目录下创建frame_anim.xml

创建frame_anim.xml

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

    <!-- animation-list 帧动画 -->
    <!-- android:oneshot的值为 false代表播放多次,true代表只播放一次 -->
    <!-- duration代表每张图片的播放时间 ,定义一个持续时间为50毫秒的动画帧 -->
    <item
        android:drawable="@drawable/img00"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img01"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img02"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img03"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img04"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img05"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img06"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img07"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img08"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img09"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img10"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img11"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img12"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img13"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img14"
        android:duration="300"/>
    <item
        android:drawable="@drawable/img15"
        android:duration="300"/>
</animation-list>

播放和暂停的布局

修改res目录下的layout目录下的activity_main.xml文件

布局配置activity_main.xml

activity_main.xml:

<RelativeLayout 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"
    tools:context="com.example.myapplication.MainActivity" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    <!-- android:background="@drawable/frame_anim" -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:padding="10dp" >

        <Button
            android:id="@+id/start"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="播放" />

        <Button
            android:id="@+id/stop"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止" />
    </LinearLayout>

</RelativeLayout>

调用逐帧动画

编写MainActivity.java

package com.example.myapplication;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;


public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView imageView;
    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);

        setXml2FrameAnim1();
        // setXml2FrameAnim2();

    }

    /**
     * 通过XML添加帧动画方法一
     */
    private void setXml2FrameAnim1() {

        // 把动画资源设置为imageView的背景,也可直接在XML里面设置
        imageView.setBackgroundResource(R.drawable.frame_anim);
        animationDrawable = (AnimationDrawable) imageView.getBackground();
    }

    /**
     * 通过XML添加帧动画方法二
     */
    private void setXml2FrameAnim2() {

        // 通过逐帧动画的资源文件获得AnimationDrawable示例
        animationDrawable = (AnimationDrawable) getResources().getDrawable(
                R.drawable.frame_anim);
        imageView.setBackground(animationDrawable);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.start:
                if (animationDrawable != null && !animationDrawable.isRunning()) {
                    animationDrawable.start();
                }
                break;
            case R.id.stop:
                if (animationDrawable != null && animationDrawable.isRunning()) {
                    animationDrawable.stop();
                }
                break;

            default:
                break;
        }
    }

}

效果演示

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
### 回答1: Android Studio中的逐帧动画是指将一系列静态图像按照一定的顺序快速播放,形成动画效果。在Android Studio中,可以使用AnimationDrawable类来实现逐帧动画。具体步骤如下: 1. 在res/drawable目录下创建一个XML文件,用于定义逐帧动画。例如,可以创建一个名为animation.xml的文件。 2. 在XML文件中,使用<animation-list>标签定义逐帧动画。在<animation-list>标签中,使用<item>标签定义每一的图像。例如,可以使用以下代码定义一个逐帧动画,其中包含三张图像: <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/frame1" android:duration="100" /> <item android:drawable="@drawable/frame2" android:duration="100" /> <item android:drawable="@drawable/frame3" android:duration="100" /> </animation-list> 3. 在Java代码中,使用AnimationDrawable类加载XML文件,并将其设置为ImageView的背景。例如,可以使用以下代码实现逐帧动画: ImageView imageView = findViewById(R.id.imageView); AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation); imageView.setBackground(animationDrawable); animationDrawable.start(); 4. 运行应用程序,即可看到逐帧动画效果。 需要注意的是,逐帧动画可能会占用较多的内存和CPU资源,因此应该谨慎使用。如果需要实现复杂的动画效果,建议使用属性动画动画。 ### 回答2: Android Studio是一个用于开发Android应用程序的集成开发环境(IDE)。Android Studio内置了许多功能强大的工具和组件,包括逐帧动画的支持。 逐帧动画是一种动画效果,在Android应用程序中经常被使用。它是通过连续显示一系列不同的图像来产生动画效果的。在Android Studio中,我们可以使用逐帧动画来为应用程序添加各种各样的动画效果。 要在Android Studio中创建一个逐帧动画,我们首先需要准备一系列的图像。这些图像可以是不同的PNG或JPEG图像文件,或者是通过Android Studio内置的绘图工具绘制出来的。然后,我们可以在res目录下创建一个XML文件,来定义逐帧动画的属性和序列。 在XML文件中,我们可以指定逐帧动画播放属性,例如重复次数、播放间隔等。然后,我们可以使用`<item>`标签来定义每一的图像资源。可以通过`<bitmap>`标签指定图像资源的来源,或者使用`<drawable>`标签指定图像资源的文件名。在`<item>`标签之间可以设置之间的过渡效果,例如渐变、平移等。 在代码中,我们可以使用`AnimationDrawable`类来加载并播放逐帧动画。我们可以通过`AnimationDrawable.addFrame()`方法添加每一的图像资源,通过`AnimationDrawable.setOneShot()`方法设置是否只播放一次,通过`AnimationDrawable.start()`方法开始播放,通过`AnimationDrawable.stop()`方法停止播放。 总结起来,Android Studio提供了逐帧动画的创建和播放的支持,通过准备一系列图像和定义动画属性,在代码中使用`AnimationDrawable`类加载并播放逐帧动画。这样,我们就可以为我们的Android应用程序添加生动有趣的动画效果。 ### 回答3: Android Studio是一个非常强大的集成开发环境,它提供了丰富的工具和功能帮助开发者进行Android应用的开发。其中的逐帧动画是一种常用的动画效果,它由一系列连续的静态图像组成,通过快速连续地播放这些图像,以达到动态的效果。 在Android Studio中创建逐帧动画非常简单。首先,我们需要准备好一组连续的静态图像,通常是位图或Drawable资源。然后,在res目录下创建一个名为"anim"的文件夹,并在该文件夹中创建一个XML文件,作为逐帧动画的描述文件。 在这个XML文件中,我们可以使用`<animation-list>`元素来定义逐帧动画。通过`<item>`元素,我们可以指定每一的图像资源,并设置持续时间。例如: ``` <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/frame1" android:duration="100" /> <item android:drawable="@drawable/frame2" android:duration="100" /> <item android:drawable="@drawable/frame3" android:duration="100" /> ... </animation-list> ``` 在这个例子中,我们指定了三个,分别是frame1、frame2和frame3,并设置每一的持续时间为100毫秒。 完成逐帧动画的XML定义后,我们可以在布局文件或代码中使用`<ImageView>`元素来显示逐帧动画。通过设置`android:src`属性为我们创建的逐帧动画的XML文件,然后调用`start()`方法开始播放动画。 ``` <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/animation" /> ``` ```java ImageView imageView = findViewById(R.id.imageView); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); animationDrawable.start(); ``` 这样,我们就可以在Android应用中展示逐帧动画了。可以通过调整每一的图像和持续时间,来实现各种不同的动画效果,丰富我们的应用界面。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lazy_Goat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值