先看下效果:

写个进度条调用类:

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() {@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);}/*** 设置背景透明度** @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;}/*** 设置下方文字 默认白色** @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);}@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* 增加了一个正方形显示* 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 转化工具**/
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);}}

看不懂得可以直接下载源码运行即可

点击下载源码

Android开发之自定义菊花进度条对话框相关推荐

  1. Android中用图片自定义一个进度条(实现蒙板效果)

    问题概述 对于进度条我相信大家不陌生,这里我就不再多说什么了.因为这个不是重点.我们要说的是如何去自定义一个不一样的进度条.这里用到两张图片(背景和前景),其实是三张(背景.前景和蒙图).当我们的蒙图 ...

  2. 【Android 应用开发】 自定义 圆形进度条 组件

    转载著名出处 : http://blog.csdn.net/shulianghan/article/details/40351487 代码下载 : -- CSDN 下载地址 : http://down ...

  3. Android中下载进度条格式,Android开发如何实现文件下载进度条

    释放双眼,带上耳机,听听看~! 今天和小编狗在这里学习下载进度的实现吧,这段代码是网上找的,自己做了些小改,通过模拟器测试.文件下载进度条控制(就是为了高清壁纸加个进度条),自己研究了好久,但是进度条 ...

  4. android简单进度条对话框,android进度条对话框实例

    郑州app开发android进度条对话框实例.下面是java代码. package cn.xhhkj.cyd; import androidx.appcompat.app.AppCompatActiv ...

  5. android 环形时间显示_Android_Android实现自定义圆形进度条,今天无意中发现一个圆形进度 - phpStudy...

    Android实现自定义圆形进度条 今天无意中发现一个圆形进度,想想自己实现一个,如下图: 基本思路是这样的: 1.首先绘制一个实心圆 2.绘制一个白色实心的正方形,遮住实心圆 3.在圆的中心动态绘制 ...

  6. Android自定义圆形进度条

    Android自定义圆形进度条 github地址:https://github.com/opq1289/CircleProgressView 效果图: 无动画: 有动画: 整圆: 切割圆: 具体步骤: ...

  7. android 自定义背景园,Android 自定义ProgressBar 进度条颜色和背景颜色

    Android 自定义ProgressBar 进度条颜色和背景颜色 首先,在drawable目录下新建文件 personal_center_level_progress_bg.xmlandroid a ...

  8. Android 自定义View,自定义属性--自定义圆形进度条(整理)

    很多的时候,系统自带的View满足不了我们的功能需求,那么我们就需要自定义View来满足我们的需求 自定义View时要先继承View,添加类的构造方法,重写父类View的一些方法,例如onDraw,为 ...

  9. android绘制环形进度_Android动态自定义圆形进度条

    这篇文章主要介绍了Android动态自定义圆形进度条,需要的朋友可以参考下 效果图: A.绘制圆环,圆弧,文本 //1.画圆环 //原点坐标 float circleX = width / 2; fl ...

最新文章

  1. vb6实现union数据结构_数据结构与算法——并查集(不相交集合)
  2. 静态方法-应用场景和定义方式
  3. java 反向映射,如何使用lambdas實現反向映射Java?
  4. android内置t卡中预制资源,[FAQ17514][Recovery]Recovery mode FAQ搜寻指南
  5. (操作系统题目题型总结)第四章:存储管理
  6. mysql优化php面试_php面试专题---18、MySQL查询优化考点
  7. Seesion工作原理
  8. Java实现二进制转换16进制(可以去掉空格)
  9. AttributeError lxml.etree Element object has no attribute get_attribute
  10. 强烈推荐!大数据领域的顶级开源工具大集合
  11. Jetson Nano 读取 GPS数据(TTL转 USB)
  12. gege.fans上热搜背后是明星私域流量的折射
  13. Java url链接生成二维码
  14. linux系统的特点有什么
  15. 用户画像分析有哪些方法可以实现?
  16. Supervisor 命令
  17. 电话自动拨号自动录音
  18. ROS学习笔记(一):利用节点进行发布与订阅
  19. 苹果笔记本中重启php,mac 启动|重启服务
  20. 7-1 jmu-JavaPython-统计文字中的单词数量并按出现次数排序 (25 分)

热门文章

  1. Nexys4DDR+OV7670实现图像灰度显示系统
  2. 计算机显示有可移动存储,winxp系统中我的电脑出现很多个可移动磁盘怎么办
  3. matlab波形振幅,MATLAB正交振幅调制解调仿真分析(一)
  4. python设置行号_Python_添加行号
  5. 使用ubuntu过程中遇到的问题汇总
  6. 线段树(结构体建法_QAQ)
  7. Python-条件控制及循环
  8. RD自身修养 满招损谦受益
  9. 轻松取得建表和索引的DDL语句
  10. winform中UI设计分辨率问题