十八、动画-帧动画
一、帧动画
1.创建xml动态文件,我这里创建的是 frame.xml
2.使用 animation-list 将图片进行定位,并通过 android:duration 设置播放的时间
<?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.创建主窗体页面
<?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();
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.效果图


浙公网安备 33010602011771号