Android 使用简单帧动画实现一个自定义动态等待层
程序员文章站
2022-03-25 21:55:57
...
前言
项目中有时候需要自定义等待层,使用帧动画实现一个动态的等待层还是不错的。
一、简单帧动画实现
1.在drawable下创建loading_view.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/loading_001" android:duration="50"/>
<item android:drawable="@drawable/loading_002" android:duration="50"/>
<item android:drawable="@drawable/loading_003" android:duration="50"/>
</animation-list>
2.在xml布局中调用loading_view
<ImageView
android:id="@+id/img_loading_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/loading_view" />
3、在代码中调用
ImageView imgLoadingView = (ImageView) view.findViewById(R.id.img_loading_view);
AnimationDrawable animationDrawable = (AnimationDrawable) imgLoadingView.getDrawable();
animationDrawable.start();//开启动画
animationDrawable.stop();//关闭动画
二、帧动画等待层
只需要自定义一个dialog,xml布局中使用以上帧动画即可。
当开启等待层时调用:
animationDrawable.start();//开启动画
当关闭等待层时调用:
animationDrawable.stop();//关闭动画