Implement a simple frame by frame animation in Android (with code download)

Keywords: Android xml encoding Programming

scene

In Android, frame by frame animation is composed of consecutive pictures.

Effect

 

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

First, prepare a group of photos with different expressions, put them under res/drawable, and then create a new animation resource file, fairy.xml, in this directory

<?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>

 

This is frame by frame animation, so the node is animation list.

Then go to the layout file, set the layout to LinearLayout and add the id attribute, and set the background to the animation resource file added above

<?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:id="@+id/linearLayout"
    android:orientation="vertical"
    android:background="@drawable/fairy"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

 

</LinearLayout>

 

Then go to the corresponding Activity, create the identification variable Flag, get the AnimationDrawable object, and add a click event for the layout manager. This controls the stop and playback of the animation.

package com.badao.animationtest;

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 AppCompatActivity {

    private boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout linearLayout= (LinearLayout) findViewById(R.id.linearLayout); //Get layout manager
        //Obtain AnimationDrawable object
        final AnimationDrawable anim= (AnimationDrawable) linearLayout.getBackground();
        linearLayout.setOnClickListener(new View.OnClickListener() {  //Add click event for layout manager
            @Override
            public void onClick(View v) {
                if(flag){
                    anim.start(); //Start animation
                    flag=false;
                }else {
                    anim.stop();  //Stop playing animation
                    flag=true;
                }
            }
        });
    }
}

 

Code download

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12097211

Posted by Bah! Name on Sat, 11 Jan 2020 07:08:12 -0800