不多说直接上代码 1、实现类 public class ProgressDialog extends Dialog implements DialogInterface.OnCancelListener { private WeakReference mContext = new WeakReference<>(null); private volatile static ProgressDialog sDialog; private static AnimationDrawable animationDrawable; private ImageView imageView; private ProgressDialog(Context context, CharSequence message) { super(context, R.style.CustomProgressDialog); mContext = new WeakReference<>(context); @SuppressLint("InflateParams") View view = LayoutInflater.from(context).inflate(R.layout.layout_load_image, null); imageView = view.findViewById(R.id.img_show); animationDrawable = (AnimationDrawable) imageView.getBackground(); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); addContentView(view, lp); setOnCancelListener(this); } @Override public void onCancel(DialogInterface dialog) { // 点手机返回键等触发Dialog消失,应该取消正在进行的网络请求等 Context context = mContext.get(); if (context != null) { Toast.makeText(context, "cancel", Toast.LENGTH_SHORT).show(); } } public static synchronized void showLoading(Context context, CharSequence message, boolean cancelable) { if (sDialog != null && sDialog.isShowing()) { sDialog.dismiss(); } if (context == null || !(context instanceof Activity)) { return; } sDialog = new ProgressDialog(context, message); sDialog.setCancelable(cancelable); if (sDialog != null && !sDialog.isShowing() && !((Activity) context).isFinishing()) { sDialog.show(); animationDrawable.start(); } } public static synchronized void stopLoading() { if (sDialog != null && sDialog.isShowing()) { sDialog.dismiss(); animationDrawable.stop(); } sDialog = null; } }
2、dialog布局 layout_load_image
<?xml version="1.0" encoding="utf-8"?>
3、动画帧图集 drawable目录存放对应图片
<?xml version="1.0" encoding="utf-8"?>