帧动画——FrameAnimation
将一系列图片有序播放,形成动画的效果。其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用
1,在Drawable文件夹下,创建animation-list为根节点的资源文件(drawable_anim)
<animation-listandroid:oneshot="false">
<item android:drawable="@mipmap/progress_1"android:duration="50"/>
<item android:drawable="@mipmap/progress_2"android:duration="50"/>
<item android:drawable="@mipmap/progress_3"android:duration="50"/>
<item android:drawable="@mipmap/progress_4"android:duration="50"/>
</animation-list>
oneshot:是否只播放一次
drawable:一帧引用的图片
duration:一帧播放的时间
2,imageview中设置背景(R.layout.anim_drawable_dialog_layout)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/refreshing_drawable_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:scaleType="fitCenter" android:src="@drawable/drawable_anim" /> </RelativeLayout>
3,继承AlertDialog,设置样式
获得image的animationDrawable(progressImg.getDrawable())
如果image设置的是background则(progressImg .getBackground())
animationDrawable.start,开始动画
public class AnimDrawableAlertDialog extends AlertDialog { private ImageView progressImg; //帧动画 private AnimationDrawable animation; public AnimDrawableAlertDialog(Context context) { super(context, R.style.MyDialog); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.anim_drawable_dialog_layout); //点击imageview外侧区域,动画不会消失 setCanceledOnTouchOutside(false); progressImg = (ImageView) findViewById(R.id.refreshing_drawable_img); //加载动画资源 animation = (AnimationDrawable) progressImg.getDrawable(); } /** * 在AlertDialog的 onStart() 生命周期里面执行开始动画 */ @Override protected void onStart() { super.onStart(); animation.start(); } /** * 在AlertDialog的onStop()生命周期里面执行停止动画 */ @Override protected void onStop() { super.onStop(); animation.stop(); } }
4,设置dialog样式(R.style.MyDialog)
<resources> <!-- 自定义dialog背景全透明无边框 --> <style name="MyDialog" parent="android:style/Theme.Dialog"> <!-- 背景颜色及和透明程度 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 是否去除标题 --> <item name="android:windowNoTitle">true</item> <!-- 是否去除边框 --> <item name="android:windowFrame">@null</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 是否模糊 --> <item name="android:backgroundDimEnabled">false</item> </style> </resources>
5,activity中调用对话框
AnimDrawableAlertDialog progressDrawableAlertDialog = new AnimDrawableAlertDialog(this); progressDrawableAlertDialog.show();
6,效果