安卓:帧动画

一.实现效果
在这里插入图片描述
二、涉及知识点
1、活动(Activity)
2、按钮(Button)
3、图像视图(ImageView)
4、动画可绘制对象(AnimationDrawable)
三.实现步骤
1.界面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/anim"
        android:src="@drawable/animation_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/bottom1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开始"
            android:onClick="onStartA"/>
        <Button
            android:id="@+id/bottom2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="停止"
            android:onClick="onStopA"/>
    </LinearLayout>

</LinearLayout>

添加两个按钮来对动画进行控制

2.绘制动画
在drawable里添加图片
在这里插入图片描述
创建animation_list绘制动画

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/fat_po_f01"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f02"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f03"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f04"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f05"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f06"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f07"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f08"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f09"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f10"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f11"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f12"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f13"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f14"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f15"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f16"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f17"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f18"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f19"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f20"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f21"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f22"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f23"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f24"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f01"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f01"
        android:duration="50"/>
    <item
        android:drawable="@drawable/fat_po_f01"
        android:duration="50"/>
</animation-list>

其中duration=“50“是指图片间的间隔时间
3.MainActivity

package net.hw.donghua;

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

public class MainActivity extends Activity {
    private ImageView iv;
    private Button button1;
    private Button button2;
    private Handler handler;
    private int n=0;
    private AnimationDrawable anim;

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

        iv = findViewById(R.id.anim);
        button1 = findViewById(R.id.bottom1);
        button2 = findViewById(R.id.bottom2);

        iv.setImageResource(R.drawable.animation_list);
        //加载动画资源文件
        anim = (AnimationDrawable)iv.getDrawable();
     
    }
    public void onStartA(View v) {
        anim.start();
    }

    public void onStopA(View v) {
        anim.stop();
    }
}

设置号播放和暂停,在主界面资源文件里设置点击,当我们点击开始就能播放动画了

猜你喜欢

转载自blog.csdn.net/weixin_41858200/article/details/83718703