十八、动画-帧动画

一、帧动画

1.创建xml动态文件,我这里创建的是 frame.xml

2.使用 animation-list 将图片进行定位,并通过 android:duration 设置播放的时间

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pic1" android:duration="100"/>
    <item android:drawable="@drawable/pic2" android:duration="100"/>
    <item android:drawable="@drawable/pic3" android:duration="100"/>
    <item android:drawable="@drawable/pic4" android:duration="100"/>
    <item android:drawable="@drawable/pic5" android:duration="100"/>
    <item android:drawable="@drawable/pic6" android:duration="100"/>
    <item android:drawable="@drawable/pic7" android:duration="100"/>
    <item android:drawable="@drawable/pic8" android:duration="100"/>
</animation-list>

3.创建主窗体页面

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearlayout1"
    android:orientation="vertical"
    android:background="@drawable/frame"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

4.动画的启动和停止

//获取动画的Drawable资源

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

//启动动画

anim.start();

//停止动画

anim.stop();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
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 ly= findViewById(R.id.linearlayout1);
 
        AnimationDrawable anim =(AnimationDrawable)ly.getBackground();
 
        ly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag){
                    anim.start();
                    flag=false;
                }
                else {
                    anim.stop();
                    flag=true;
                }
            }
        });
    }
}

5.效果图

 

posted @   搬砖工具人  阅读(88)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示