Android开发之自定义菊花进度条对话框
生活随笔
收集整理的這篇文章主要介紹了
Android开发之自定义菊花进度条对话框
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
先看下效果:
寫個(gè)進(jìn)度條調(diào)用類:
package com.xiayiye.yhsh.flowerdialog;import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface;/*** 網(wǎng)絡(luò)請求加載對話框以及普通的alertDialog*/ public class DialogUtils {private Context mContext;private LoadingDialog loadingDialog;public DialogUtils(Context context) {this.mContext = context;}/*** 顯示菊花以及菊花下方的文字提示,點(diǎn)擊外部不可取消,點(diǎn)擊返回可以取消* 不接收回調(diào)接收回調(diào)*/public void showLoadingWithLabel(String text) {loadingDialog = LoadingDialog.create(mContext).setLabel(text).show();}/*** 顯示菊花以及菊花下方的文字提示,點(diǎn)擊外部不可取消,點(diǎn)擊返回可以取消* 接收回調(diào)*/public void showLoadingWithLabel(String text, DialogInterface.OnCancelListener onCancelListener) {loadingDialog = LoadingDialog.create(mContext).setLabel(text).setCancellableListener(onCancelListener).show();}/*** @Param cancelable 設(shè)置為false 返回按鈕不可用 若為true 直接調(diào)用{@link #showLoadingWithLabel}的監(jiān)聽方法* 顯示菊花以及菊花下方的文字提示,點(diǎn)擊外部不可取消,點(diǎn)擊返回可以取消* 不接收回調(diào)接收回調(diào)*/public void showLoadingWithLabel(String text, boolean cancelable) {loadingDialog = LoadingDialog.create(mContext).setLabel(text).setCancellable(cancelable).show();}/*** 僅顯示一個(gè)菊花 不接收取消回調(diào)* 默認(rèn)點(diǎn)擊外部不可取消 ,點(diǎn)擊返回按鈕可以dismiss*/public void showLoading() {loadingDialog = LoadingDialog.create(mContext).show();}/*** 僅顯示一個(gè)菊花 并且有 cancel回調(diào)* 默認(rèn)點(diǎn)擊外部不可取消 ,點(diǎn)擊返回按鈕可以dismiss*/public void showLoading(DialogInterface.OnCancelListener onCancelListener) {loadingDialog = LoadingDialog.create(mContext).setCancellableListener(onCancelListener).show();}/*** @Param cancelable 設(shè)置為false 返回按鈕不可用 若為true 直接調(diào)用{@link #showLoading}的監(jiān)聽方法* 顯示菊花,點(diǎn)擊外部不可取消* 不接收回調(diào)*/public void showLoading(boolean cancelable) {loadingDialog = LoadingDialog.create(mContext).setCancellable(cancelable).show();}/*** dismiss*/public void dismissLoading() {if (loadingDialog!=null)loadingDialog.dismiss();}/*** 無title 一個(gè)positivebutton 點(diǎn)擊外部以及返回按鈕均不可取消* 點(diǎn)擊button消失** @param message* @param textPositiveButton* @param onDismissListener null時(shí)不監(jiān)聽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() {@Overridepublic 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);}/*** 設(shè)置背景透明度** @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)}*/@Deprecatedpublic 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;}/*** 設(shè)置下方文字 默認(rèn)白色** @param label* @return*/public LoadingDialog setLabel(String label) {mProgressDialog.setLabel(label);return this;}/*** 設(shè)置文字及其顏色** @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;}/*** 設(shè)置是否可取消** @param isCancellable* @return*/public LoadingDialog setCancellable(boolean isCancellable) {mProgressDialog.setCancelable(isCancellable);mProgressDialog.setOnCancelListener(null);return this;}/*** 設(shè)置取消監(jiān)聽** @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);}@Overrideprotected 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* 增加了一個(gè)正方形顯示* 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);}//正方形顯示@Overrideprotected 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 轉(zhuǎn)化工具**/ 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);}}看不懂得可以直接下載源碼運(yùn)行即可
點(diǎn)擊下載源碼
總結(jié)
以上是生活随笔為你收集整理的Android开发之自定义菊花进度条对话框的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android开发之ApiCloud轮播
- 下一篇: TechhInsights:iSIM 将