19. 帧动画

19. 帧动画

把几张图片进行快速播放形成的动画。

19.1 素材准备

在这里插入图片描述

8张图

19.2 创建animation-list集合

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/frame1" android:duration="120"/>
    <item android:drawable="@drawable/frame2" android:duration="120"/>
    <item android:drawable="@drawable/frame3" android:duration="120"/>
    <item android:drawable="@drawable/frame4" android:duration="120"/>
    <item android:drawable="@drawable/frame5" android:duration="120"/>
    <item android:drawable="@drawable/frame6" android:duration="120"/>
    <item android:drawable="@drawable/frame7" android:duration="120"/>
    <item android:drawable="@drawable/frame8" android:duration="120"/>
</animation-list>

在这里插入图片描述

19.3 编写布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/frame"/>

19.4 动画的启动和停止
package com.dingjiaxiong.myzhuzhendonghua;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    //标志位
    private boolean flag = true;

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

        RelativeLayout relativeLayout = findViewById(R.id.rl);

        AnimationDrawable anim = (AnimationDrawable) relativeLayout.getBackground();

        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag){
                    anim.start();
                    flag = false;
                }else{
                    anim.stop();
                    flag = true;
                }
            }
        });
    }
}


运行


posted @ 2022-09-19 07:48  随遇而安==  阅读(17)  评论(0编辑  收藏  举报