先看下效果:
写个进度条调用类:
package com.xiayiye.yhsh.flowerdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
/**
* 网络请求加载对话框以及普通的alertDialog
*/
public class DialogUtils {
private Context mContext;
private LoadingDialog loadingDialog;
public DialogUtils(Context context) {
this.mContext = context;
}
/**
* 显示菊花以及菊花下方的文字提示,点击外部不可取消,点击返回可以取消
* 不接收回调接收回调
*/
public void showLoadingWithLabel(String text) {
loadingDialog = LoadingDialog.create(mContext)
.setLabel(text)
.show();
}
/**
* 显示菊花以及菊花下方的文字提示,点击外部不可取消,点击返回可以取消
* 接收回调
*/
public void showLoadingWithLabel(String text, DialogInterface.OnCancelListener onCancelListener) {
loadingDialog = LoadingDialog.create(mContext)
.setLabel(text)
.setCancellableListener(onCancelListener)
.show();
}
/**
* @Param cancelable 设置为false 返回按钮不可用 若为true 直接调用{@link #showLoadingWithLabel}的监听方法
* 显示菊花以及菊花下方的文字提示,点击外部不可取消,点击返回可以取消
* 不接收回调接收回调
*/
public void showLoadingWithLabel(String text, boolean cancelable) {
loadingDialog = LoadingDialog.create(mContext)
.setLabel(text)
.setCancellable(cancelable)
.show();
}
/**
* 仅显示一个菊花 不接收取消回调
* 默认点击外部不可取消 ,点击返回按钮可以dismiss
*/
public void showLoading() {
loadingDialog = LoadingDialog.create(mContext)
.show();
}
/**
* 仅显示一个菊花 并且有 cancel回调
* 默认点击外部不可取消 ,点击返回按钮可以dismiss
*/
public void showLoading(DialogInterface.OnCancelListener onCancelListener) {
loadingDialog = LoadingDialog.create(mContext)
.setCancellableListener(onCancelListener)
.show();
}
/**
* @Param cancelable 设置为false 返回按钮不可用 若为true 直接调用{@link #showLoading}的监听方法
* 显示菊花,点击外部不可取消
* 不接收回调
*/
public void showLoading(boolean cancelable) {
loadingDialog = LoadingDialog.create(mContext)
.setCancellable(cancelable)
.show();
}
/**
* dismiss
*/
public void dismissLoading() {
if (loadingDialog!=null)
loadingDialog.dismiss();
}
/**
* 无title 一个positivebutton 点击外部以及返回按钮均不可取消
* 点击button消失
*
* @param message
* @param textPositiveButton
* @param onDismissListener null时不监听dismiss
*/
public void showAlertDialog(String message, String textPositiveButton, DialogInterface.OnDismissListener onDismissListener) {
if(!((Activity) mContext).isFinishing()) {
new AlertDialog.Builder(mContext)
.setCancelable(false)
.setMessage(message)
.setPositiveButton(textPositiveButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setOnDismissListener(onDismissListener)
.show();
}
}
}
2.工具类里面涉及到的LoadingDialog类如下:
package com.xiayiye.yhsh.flowerdialog;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
public class LoadingDialog {
private ProgressDialog mProgressDialog;
private float mDimAmount;
private int mWindowColor;
private float mCornerRadius;
public LoadingDialog(Context context) {
mProgressDialog = new ProgressDialog(context);
mDimAmount = 0;
mWindowColor = context.getResources().getColor(R.color.colorLoadingProgressBg);
mCornerRadius = 10;
View view = new SpinView(context);
mProgressDialog.setView(view);
}
public static LoadingDialog create(Context context) {
return new LoadingDialog(context);
}
/**
* 设置背景透明度
*
* @param dimAmount
* @return
*/
public LoadingDialog setDimAmount(float dimAmount) {
if (dimAmount >= 0 && dimAmount <= 1) {
mDimAmount = dimAmount;
}
return this;
}
/**
* @param color ARGB color
* @return Current HUD
* @deprecated As of release 1.1.0, replaced by {@link #setBackgroundColor(int)}
*/
@Deprecated
public LoadingDialog setWindowColor(int color) {
mWindowColor = color;
return this;
}
/**
* Specify the HUD background color
*
* @param color ARGB color
* @return Current HUD
*/
public LoadingDialog setBackgroundColor(int color) {
mWindowColor = color;
return this;
}
/**
* Specify corner radius of the HUD (default is 10)
*
* @param radius Corner radius in dp
* @return Current HUD
*/
public LoadingDialog setCornerRadius(float radius) {
mCornerRadius = radius;
return this;
}
/**
* 设置下方文字 默认白色
*
* @param label
* @return
*/
public LoadingDialog setLabel(String label) {
mProgressDialog.setLabel(label);
return this;
}
/**
* 设置文字及其颜色
*
* @param label
* @param color
* @return
*/
public LoadingDialog setLabel(String label, int color) {
mProgressDialog.setLabel(label, color);
return this;
}
/**
* Provide a custom view to be displayed.
*
* @param view Must not be null
* @return Current HUD
*/
public LoadingDialog setCustomView(View view) {
if (view != null) {
mProgressDialog.setView(view);
} else {
throw new RuntimeException("Custom view must not be null!");
}
return this;
}
/**
* 设置是否可取消
*
* @param isCancellable
* @return
*/
public LoadingDialog setCancellable(boolean isCancellable) {
mProgressDialog.setCancelable(isCancellable);
mProgressDialog.setOnCancelListener(null);
return this;
}
/**
* 设置取消监听
*
* @param listener
* @return
*/
public LoadingDialog setCancellableListener(DialogInterface.OnCancelListener listener) {
mProgressDialog.setCancelable(null != listener);
mProgressDialog.setOnCancelListener(listener);
return this;
}
public LoadingDialog show() {
if (!isShowing()) {
mProgressDialog.show();
}
return this;
}
public boolean isShowing() {
return mProgressDialog != null && mProgressDialog.isShowing();
}
public void dismiss() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
private class ProgressDialog extends Dialog {
private View mView;
private TextView mLabelText;
private String mLabel;
private FrameLayout mCustomViewContainer;
private BackgroundLayout mBackgroundLayout;
private int mLabelColor = Color.WHITE;
public ProgressDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.loading_progress_layout);
Window window = getWindow();
window.setBackgroundDrawable(new ColorDrawable(0));
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.dimAmount = mDimAmount;
layoutParams.gravity = Gravity.CENTER;
window.setAttributes(layoutParams);
setCanceledOnTouchOutside(false);
initViews();
}
private void initViews() {
mBackgroundLayout = (BackgroundLayout) findViewById(R.id.background);
mBackgroundLayout.setBaseColor(mWindowColor);
mBackgroundLayout.setCornerRadius(mCornerRadius);
mCustomViewContainer = (FrameLayout) findViewById(R.id.container);
addViewToFrame(mView);
mLabelText = (TextView) findViewById(R.id.label);
setLabel(mLabel, mLabelColor);
}
private void addViewToFrame(View view) {
if (view == null) return;
int wrapParam = ViewGroup.LayoutParams.WRAP_CONTENT;
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(wrapParam, wrapParam);
mCustomViewContainer.addView(view, params);
}
public void setView(View view) {
if (view != null) {
mView = view;
if (isShowing()) {
mCustomViewContainer.removeAllViews();
addViewToFrame(view);
}
}
}
public void setLabel(String label) {
mLabel = label;
if (mLabelText != null) {
if (label != null) {
mLabelText.setText(label);
mLabelText.setVisibility(View.VISIBLE);
} else {
mLabelText.setVisibility(View.GONE);
}
}
}
public void setLabel(String label, int color) {
mLabel = label;
mLabelColor = color;
if (mLabelText != null) {
if (label != null) {
mLabelText.setText(label);
mLabelText.setTextColor(color);
mLabelText.setVisibility(View.VISIBLE);
} else {
mLabelText.setVisibility(View.GONE);
}
}
}
}
}
LoadingDialog涉及到的BackgroundLayout类
package com.xiayiye.yhsh.flowerdialog;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.LinearLayout;
/**
* from https://github.com/Kaopiz/KProgressHUD
* 增加了一个正方形显示
* update minionshuang
*/
public class BackgroundLayout extends LinearLayout {
private float mCornerRadius;
private int mBackgroundColor;
public BackgroundLayout(Context context) {
super(context);
init();
}
public BackgroundLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public BackgroundLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@SuppressWarnings("deprecation")
private void init() {
int color = getContext().getResources().getColor(R.color.colorLoadingProgressBg);
initBackground(color, mCornerRadius);
}
private void initBackground(int color, float cornerRadius) {
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setColor(color);
drawable.setCornerRadius(cornerRadius);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(drawable);
} else {
setBackgroundDrawable(drawable);
}
}
public void setCornerRadius(float radius) {
mCornerRadius = ScaleUtils.dip2px(radius);
initBackground(mBackgroundColor, mCornerRadius);
}
public void setBaseColor(int color) {
mBackgroundColor = color;
initBackground(mBackgroundColor, mCornerRadius);
}
//正方形显示
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
int size = Math.max(width, height);
setMeasuredDimension(size, size);
}
}
BackgroundLayout里面涉及到的尺寸工具类ScaleUtils
package com.xiayiye.yhsh.flowerdialog;
import android.content.res.Resources;
/**
* dp px sp 转化工具
*
*/
public class ScaleUtils {
private ScaleUtils() {
}
public static int dip2px(float f) {
return Math.round((Resources.getSystem().getDisplayMetrics().density * f) + 0.5f);
}
public static int px2dip(float f) {
return Math.round((f / Resources.getSystem().getDisplayMetrics().density) + 0.5f);
}
public static int px2sp(float f) {
return Math.round((f / Resources.getSystem().getDisplayMetrics().scaledDensity) + 0.5f);
}
public static int sp2px(float f) {
return Math.round((Resources.getSystem().getDisplayMetrics().scaledDensity * f) + 0.5f);
}
}
看不懂得可以直接下载源码运行即可
点击下载源码