之前通过ProgressBar实现一个加载中的圆形进度条,因为是ProgressBar自带的旋转效果,在高版本的弹窗中并不是匀速转动的,所以稍作修改,通过rotate旋转动画,实现自定义加载框的效果。
效果图:
一、rotate动画如下注释详细,不在赘述:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android">
<!--
android:interpolator:这个属性是用来设置转动速率的。
LinearInterpolator为匀速效果,Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果,
android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止
android:duration属性表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。
android:startOffset 在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行
android:repeatMode 重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。
在android:repeatCount大于0或为infinite时生效
android:detachWallpaper 表示是否在壁纸上运行
android:zAdjustment 表示被animated的内容在运行时在z轴上的位置,默认为normal。normal保持内容当前的z轴顺序top运行时在最顶层显示bottom运行时在最底层显示
-->
<rotate
android:duration="1500"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:toDegrees="359"
android:visible="true" />
</rotate>
二、自定义弹窗控件,LoadingDialog继承自Dialog,
通过自定义一个布局,将动画,放到一个ImageView控件上。对外提供:showDialog()、dismissDialog()、isShowing()三个方法,在调用的时候,可以在需要的地方随时弹出、关闭弹窗方便、灵活。
/**
* 自定义加载进度对话框
* Created by zxf on 2019-01-16.
*/
public class LoadingDialog extends Dialog {
private Animation animation;
private TextView textView;
private boolean isShowing = false;
private final ImageView mImgLoading;
public LoadingDialog(Context context) {
super(context);
//设置对话框背景透明
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
setContentView(R.layout.layout_loading);
mImgLoading = findViewById(R.id.img_loading);
textView = (TextView) findViewById(R.id.loading_message);
setCanceledOnTouchOutside(false);
animation = AnimationUtils.loadAnimation(context, R.anim.anim_rotate);
}
/**
* 为加载进度个对话框设置不同的提示消息
* @param message 给用户展示的提示信息
* @return build模式设计,可以链式调用
*/
public LoadingDialog setMessage(String message) {
textView.setText(message);
return this;
}
/**
* 关闭对话框
*/
public void dismissDialog() {
dismiss();
}
/**
* 显示对话框
*/
public void showDialog() {
mImgLoading.setAnimation(animation);
mImgLoading.startAnimation(animation);
show();
}
public boolean isShowing() {
return isShowing;
}
}
下附Demo项目地址:
https://download.youkuaiyun.com/download/zxf_2016/10920842