安卓开发学习笔记(14)逐帧动画(实例:一个行走的小人)

注意:边看视频边敲代码,一样的步骤又出现了问题,在新建xml文件时要建在drawable目录下,不要建在anim下,不然会出错

第一步:先添加图片在drawable的mdpi下

第二步:创建fairy.xml在drawable目录下

<?xml version="1.0" encoding="utf-8"?>
<!--使用逐帧动画,要将set修改为animation-list,每张图片显示60毫秒-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/peo1" android:duration="60"/>
    <item android:drawable="@drawable/peo2" android:duration="60"/>
    <item android:drawable="@drawable/peo3" android:duration="60"/>
    <item android:drawable="@drawable/peo4" android:duration="60"/>
    <item android:drawable="@drawable/peo5" android:duration="60"/>
    <item android:drawable="@drawable/peo6" 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"
    android:orientation="vertical"
    android:background="@drawable/fairy"//添加动画资源文件
    android:id="@+id/linearlayout"
    tools:context=".MainActivity">
</LinearLayout>

第四步:控制动画的开始与停止MainActivity.java

package com.example.animafairy;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
private boolean flag=true;//记录播放状态
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout linearLayout=findViewById(R.id.linearlayout);
        final AnimationDrawable anim= (AnimationDrawable) linearLayout.getBackground();//获取动画Drawable资源
        
        //为布局管理器添加单击事件,实现单击屏幕开始动画和结束动画
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag)//如果动画为真,就播放动画
                {
                    anim.start();//播放动画
                    flag=false;
                }else
                {
                    anim.stop();//停止动画
                    flag=true;
                }
            }
        });
    }
}

运行结果

设置横屏显示

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.animafairy">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:screenOrientation="landscape">//横屏代码
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章