我们在项目中经常会遇到这样一个应用场景:执行某个耗时操作时,为了安抚用户等待的烦躁心情我们一般会使用进度条之类的空间,在android中让 大家最容易想到的就是progressbar或者progressDialog,区别在于前者是一个控件,后者是对话框。由于一些需求在弹出进度条时不希 望用户能够操作其他控件,所以只能使用progressDialog,这个时候有遇到了一个问题,我不想要progressDialog的黑色框框,感觉 这样跟应用的整体风格不协调,这个时候就考虑了写一个自定义的progressDialog。
         在网上搜过很多自定义progressDialog的例子,对着写了下,但是没有任何效果,不知道是自己使用的方法不对还是什么地方出错了。通过不断的查找资料,写了一个简单的自定义progressDialog。先上图看下效果:

1.String.xml 文件,progressDialog是继承与Dialog,先设置一下progressDialog的风格,设置背景透明色。

  1. <style name="CustomDialog" parent="@android:style/Theme.Dialog">
  2. <item name="android:windowFrame">@null</item>
  3. <item name="android:windowIsFloating">true</item>
  4. <item name="android:windowContentOverlay">@null</item>
  5. <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
  6. <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
  7. </style>
  8. <style name="CustomProgressDialog" parent="@style/CustomDialog">
  9. <item name="android:windowBackground">@android:color/transparent</item>
  10. <item name="android:windowNoTitle">true</item>
  11. </style>

2.customprogressdialog.xml文件,定义自己的布局,由于我的需求只需要一个进度条以及一串显示的内容,所以布局比较接单

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="horizontal">
  7. <ImageView
  8. android:id="@+id/loadingImageView"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:background="@anim/progress_round"/>
  12. <TextView
  13. android:id="@+id/id_tv_loadingmsg"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center_vertical"
  17. android:textSize="20dp"/>
  18. </LinearLayout>

3.progress_round.xml文件.这个文件为了实现转动的效果,循环显示这些图片。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animation-list
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:oneshot="false">
  5. <item android:drawable="@drawable/progress_1" android:duration="200"/>
  6. <item android:drawable="@drawable/progress_2" android:duration="200"/>
  7. <item android:drawable="@drawable/progress_3" android:duration="200"/>
  8. <item android:drawable="@drawable/progress_4" android:duration="200"/>
  9. <item android:drawable="@drawable/progress_5" android:duration="200"/>
  10. <item android:drawable="@drawable/progress_6" android:duration="200"/>
  11. <item android:drawable="@drawable/progress_7" android:duration="200"/>
  12. <item android:drawable="@drawable/progress_8" android:duration="200"/>
  13. </animation-list>

4.CustomProgressDialog.java文件,这个是就是我们最终需要使用的progressDialog了。

  1. /**************************************************************************************
  2. * [Project]
  3. *       MyProgressDialog
  4. * [Package]
  5. *       com.lxd.widgets
  6. * [FileName]
  7. *       CustomProgressDialog.java
  8. * [Copyright]
  9. *       Copyright 2012 LXD All Rights Reserved.
  10. * [History]
  11. *       Version          Date              Author                        Record
  12. *--------------------------------------------------------------------------------------
  13. *       1.0.0           2012-4-27         lxd (rohsuton@gmail.com)        Create
  14. **************************************************************************************/
  15. package com.lxd.widgets;
  16. import com.lxd.activity.R;
  17. import android.app.Dialog;
  18. import android.content.Context;
  19. import android.graphics.drawable.AnimationDrawable;
  20. import android.view.Gravity;
  21. import android.widget.ImageView;
  22. import android.widget.TextView;
  23. /********************************************************************
  24. * [Summary]
  25. *       TODO 请在此处简要描述此类所实现的功能。因为这项注释主要是为了在IDE环境中生成tip帮助,务必简明扼要
  26. * [Remarks]
  27. *       TODO 请在此处详细描述类的功能、调用方法、注意事项、以及与其它类的关系.
  28. *******************************************************************/
  29. public class CustomProgressDialog extends Dialog {
  30. private Context context = null;
  31. private static CustomProgressDialog customProgressDialog = null;
  32. public CustomProgressDialog(Context context){
  33. super(context);
  34. this.context = context;
  35. }
  36. public CustomProgressDialog(Context context, int theme) {
  37. super(context, theme);
  38. }
  39. public static CustomProgressDialog createDialog(Context context){
  40. customProgressDialog = new CustomProgressDialog(context,R.style.CustomProgressDialog);
  41. customProgressDialog.setContentView(R.layout.customprogressdialog);
  42. customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
  43. return customProgressDialog;
  44. }
  45. public void onWindowFocusChanged(boolean hasFocus){
  46. if (customProgressDialog == null){
  47. return;
  48. }
  49. ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
  50. AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
  51. animationDrawable.start();
  52. }
  53. /**
  54. *
  55. * [Summary]
  56. *       setTitile 标题
  57. * @param strTitle
  58. * @return
  59. *
  60. */
  61. public CustomProgressDialog setTitile(String strTitle){
  62. return customProgressDialog;
  63. }
  64. /**
  65. *
  66. * [Summary]
  67. *       setMessage 提示内容
  68. * @param strMessage
  69. * @return
  70. *
  71. */
  72. public CustomProgressDialog setMessage(String strMessage){
  73. TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg);
  74. if (tvMsg != null){
  75. tvMsg.setText(strMessage);
  76. }
  77. return customProgressDialog;
  78. }
  79. }

5.接下来就是写一个测试activity调用我们的progressDialog了。

  1. package com.lxd.activity;
  2. import com.lxd.widgets.CustomProgressDialog;
  3. import android.app.Activity;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.view.Window;
  7. import android.view.WindowManager;
  8. public class MainFrame extends Activity {
  9. private MainFrameTask mMainFrameTask = null;
  10. private CustomProgressDialog progressDialog = null;
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  15. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  16. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  17. setContentView(R.layout.main);
  18. mMainFrameTask = new MainFrameTask(this);
  19. mMainFrameTask.execute();
  20. }
  21. @Override
  22. protected void onDestroy() {
  23. stopProgressDialog();
  24. if (mMainFrameTask != null && !mMainFrameTask.isCancelled()){
  25. mMainFrameTask.cancel(true);
  26. }
  27. super.onDestroy();
  28. }
  29. private void startProgressDialog(){
  30. if (progressDialog == null){
  31. progressDialog = CustomProgressDialog.createDialog(this);
  32. progressDialog.setMessage("正在加载中...");
  33. }
  34. progressDialog.show();
  35. }
  36. private void stopProgressDialog(){
  37. if (progressDialog != null){
  38. progressDialog.dismiss();
  39. progressDialog = null;
  40. }
  41. }
  42. public class MainFrameTask extends AsyncTask<Integer, String, Integer>{
  43. private MainFrame mainFrame = null;
  44. public MainFrameTask(MainFrame mainFrame){
  45. this.mainFrame = mainFrame;
  46. }
  47. @Override
  48. protected void onCancelled() {
  49. stopProgressDialog();
  50. super.onCancelled();
  51. }
  52. @Override
  53. protected Integer doInBackground(Integer... params) {
  54. try {
  55. Thread.sleep(10 * 1000);
  56. } catch (InterruptedException e) {
  57. e.printStackTrace();
  58. }
  59. return null;
  60. }
  61. @Override
  62. protected void onPreExecute() {
  63. startProgressDialog();
  64. }
  65. @Override
  66. protected void onPostExecute(Integer result) {
  67. stopProgressDialog();
  68. }
  69. }
  70. }

这样我们需要的progressDialog效果就出来了,希望对遇到跟我类似问题的朋友有所帮助,里面有什么写的不对的地方请大家指教。

转载请指明出处:http://blog.csdn.net/rohsuton/article/details/7518031

最后贴上完整代码:自定义ProgressDialog

android 自定义progressDialog实现的更多相关文章

  1. (转载)Android自定义ProgressDialog进度等待框

    Android自定义ProgressDialog进度等待框 作者:无缘公子 字体:[增加 减小] 类型:转载 时间:2016-01-11我要评论 这篇文章主要介绍了Android自定义Progress ...

  2. Android 自定义ProgressDialog

    Android本身已经提供了ProgressDialog进度等待框,使用该Dialog,我们可以为用户提供更好的体验:在网络请求时,弹出此框等待网络数据. 不过,既然是为了提高用户体验,我们肯定希望该 ...

  3. Android自定义类似ProgressDialog效果的Dialog

    Android自定义类似ProgressDialog效果的Dialog. 方法如下: 1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景). 如我要的效果: 2.定义loadin ...

  4. android 自定义动画

    android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...

  5. Android 之 ProgressDialog用法介绍

    布局文件测试: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...

  6. Android自定义View 画弧形,文字,并增加动画效果

    一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类   B ...

  7. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  8. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  9. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

随机推荐

  1. asp.net Handler中的IsReusable属性及在Handler中使用Session

    大家在用HttpHandler的时候,一般都会有两个大的疑问(当然,前提是你有钻研精神的话,呵呵) 1. IsReusable到底什么意思? 老实说,这个属性很多人都感兴趣,但搞懂的人确实不多.MSD ...

  2. meta标签常用属性整理

    在segmentfault看到这篇文章,觉得整理的很详细,所以转载过来和大家分享一下. 原文地址:http://segmentfault.com/blog/ciaocc/119000000240791 ...

  3. magento错误 Service Temporarily Unavailable magento

    前台访问出现错误 Service Temporarily Unavailable magento 解决方法 Service TemporarilyUnavailable字面意思是此服务暂时无法使用,如 ...

  4. php报警:Strict Standards: Only variables should be passed by reference in

    错误原因 因为end函数的原因. end函数: mixed end    ( array &$array   ) 你可以看到end的参数是一个引用(reference),而你只能把一个变量的引 ...

  5. bzoj2127: happiness

    Description 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友 ...

  6. Communication System

    poj1018:http://poj.org/problem?id=1018 题意:某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产, ...

  7. Tengine简单安装

    跟NGINX一样,没有深入研究配置,因为暂时用不到..:) 下载2.10,基于NGINX1.6.2的. 我的配置参数如下: ./configure --with-openssl=/root/tengi ...

  8. Ext.js form 表单提交问题

    var form = new Ext.form.FormPanel({ labelAlign : 'right', border : false, bodyStyle : 'background-co ...

  9. C51 I2C接口驱动,IO口模拟I2C(主+从)

    Master.asm ;/*------------------------------------------------------------------*/ ;/* --- STC MCU I ...

  10. ARM Cortex M3系列GPIO口介绍(工作方式探讨)

    一.Cortex M3的GPIO口特性    在介绍GPIO口功能前,有必要先说明一下M3的结构框图,这样能够更好理解总线结构和GPIO所处的位置. Cortex M3结构框图     从图中可以看出 ...