利用animation-list实现帧动画

一:在drawable目录下新建just.xml文件和against.xml文件

顺序显示动画文件:just.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/img01" android:duration="400"/>
    <item android:drawable="@drawable/img02" android:duration="400"/>
    <item android:drawable="@drawable/img03" android:duration="400"/>
    <item android:drawable="@drawable/img04" android:duration="400"/>
    <item android:drawable="@drawable/img05" android:duration="400"/>
    <item android:drawable="@drawable/img06" android:duration="400"/>
    <item android:drawable="@drawable/img07" android:duration="400"/>
    <item android:drawable="@drawable/img08" android:duration="400"/>
    <item android:drawable="@drawable/img09" android:duration="400"/>
</animation-list>

倒序显示动画文件:against.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/img09" android:duration="400"/>
    <item android:drawable="@drawable/img08" android:duration="400"/>
    <item android:drawable="@drawable/img07" android:duration="400"/>
    <item android:drawable="@drawable/img06" android:duration="400"/>
    <item android:drawable="@drawable/img05" android:duration="400"/>
    <item android:drawable="@drawable/img04" android:duration="400"/>
    <item android:drawable="@drawable/img03" android:duration="400"/>
    <item android:drawable="@drawable/img02" android:duration="400"/>
    <item android:drawable="@drawable/img01" android:duration="400"/>
</animation-list>

二:布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".Activity9"
    android:padding="20dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="利用animation-list实现帧动画"
        android:textSize="23sp"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_marginTop="60dp"
        android:layout_marginBottom="80dp">
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="100dp"/>

        <Button
            android:id="@+id/btn_just"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="顺序播放"/>
        <Button
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止播放"/>
        <Button
            android:id="@+id/btn_against"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="倒序播放"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

三:调用

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

public class Activity9 extends AppCompatActivity {
    private AnimationDrawable animationDrawable;
    private Button btn_just;
    private Button btn_stop;
    private Button btn_against;
    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_9);

        initView();
    }

    private void initView() {
        Button btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btn_just = findViewById(R.id.btn_just);
        btn_stop = findViewById(R.id.btn_stop);
        btn_against = findViewById(R.id.btn_against);
        image = findViewById(R.id.image);

        btn_just.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setImageResource(R.drawable.just);
                animationDrawable = (AnimationDrawable) image.getDrawable();
                animationDrawable.start();
            }
        });
        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationDrawable = (AnimationDrawable) image.getDrawable();
                animationDrawable.stop();
            }
        });
        btn_against.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setImageResource(R.drawable.against);
                animationDrawable = (AnimationDrawable) image.getDrawable();
                animationDrawable.start();
            }
        });
    }
}

四:运行效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Qxnedy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值