android 自定义progressDialog实现

我们在项目中经常会遇到这样一个应用场景:执行某个耗时操作时,为了安抚用户等待的烦躁心情我们一般会使用进度条之类的空间,在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.       
  9.     <style name="CustomProgressDialog" parent="@style/CustomDialog">  
  10.         <item name="android:windowBackground">@android:color/transparent</item>  
  11.         <item name="android:windowNoTitle">true</item>  
  12.     </style>  
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
	<item name="android:windowFrame">@null</item>
    	<item name="android:windowIsFloating">true</item>
    	<item name="android:windowContentOverlay">@null</item>
    	<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    	<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
	</style>
	
	<style name="CustomProgressDialog" parent="@style/CustomDialog">
		<item name="android:windowBackground">@android:color/transparent</item>
    	<item name="android:windowNoTitle">true</item>
	</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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal">
    <ImageView
        android:id="@+id/loadingImageView"
       	android:layout_width="wrap_content"
       	android:layout_height="wrap_content"
       	android:background="@anim/progress_round"/>
    <TextView
        android:id="@+id/id_tv_loadingmsg"
        android:layout_width="wrap_content"
       	android:layout_height="wrap_content"
       	android:layout_gravity="center_vertical"
       	android:textSize="20dp"/>
</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>  
<?xml version="1.0" encoding="utf-8"?>
<animation-list
	xmlns:android="http://schemas.android.com/apk/res/android"
  	android:oneshot="false">
    <item android:drawable="@drawable/progress_1" android:duration="200"/>
    <item android:drawable="@drawable/progress_2" android:duration="200"/>
    <item android:drawable="@drawable/progress_3" android:duration="200"/>
    <item android:drawable="@drawable/progress_4" android:duration="200"/>
    <item android:drawable="@drawable/progress_5" android:duration="200"/>
    <item android:drawable="@drawable/progress_6" android:duration="200"/>
    <item android:drawable="@drawable/progress_7" android:duration="200"/>
    <item android:drawable="@drawable/progress_8" android:duration="200"/>
</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.       
  16. package com.lxd.widgets;  
  17.   
  18.   
  19. import com.lxd.activity.R;  
  20.   
  21. import android.app.Dialog;  
  22. import android.content.Context;  
  23. import android.graphics.drawable.AnimationDrawable;  
  24. import android.view.Gravity;  
  25. import android.widget.ImageView;  
  26. import android.widget.TextView;  
  27.   
  28.   
  29. /******************************************************************** 
  30.  * [Summary] 
  31.  *       TODO 请在此处简要描述此类所实现的功能。因为这项注释主要是为了在IDE环境中生成tip帮助,务必简明扼要 
  32.  * [Remarks] 
  33.  *       TODO 请在此处详细描述类的功能、调用方法、注意事项、以及与其它类的关系. 
  34.  *******************************************************************/  
  35.   
  36. public class CustomProgressDialog extends Dialog {  
  37.     private Context context = null;  
  38.     private static CustomProgressDialog customProgressDialog = null;  
  39.       
  40.     public CustomProgressDialog(Context context){  
  41.         super(context);  
  42.         this.context = context;  
  43.     }  
  44.       
  45.     public CustomProgressDialog(Context context, int theme) {  
  46.         super(context, theme);  
  47.     }  
  48.       
  49.     public static CustomProgressDialog createDialog(Context context){  
  50.         customProgressDialog = new CustomProgressDialog(context,R.style.CustomProgressDialog);  
  51.         customProgressDialog.setContentView(R.layout.customprogressdialog);  
  52.         customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;  
  53.           
  54.         return customProgressDialog;  
  55.     }  
  56.    
  57.     public void onWindowFocusChanged(boolean hasFocus){  
  58.           
  59.         if (customProgressDialog == null){  
  60.             return;  
  61.         }  
  62.           
  63.         ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);  
  64.         AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();  
  65.         animationDrawable.start();  
  66.     }  
  67.    
  68.     /** 
  69.      *  
  70.      * [Summary] 
  71.      *       setTitile 标题 
  72.      * @param strTitle 
  73.      * @return 
  74.      * 
  75.      */  
  76.     public CustomProgressDialog setTitile(String strTitle){  
  77.         return customProgressDialog;  
  78.     }  
  79.       
  80.     /** 
  81.      *  
  82.      * [Summary] 
  83.      *       setMessage 提示内容 
  84.      * @param strMessage 
  85.      * @return 
  86.      * 
  87.      */  
  88.     public CustomProgressDialog setMessage(String strMessage){  
  89.         TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg);  
  90.           
  91.         if (tvMsg != null){  
  92.             tvMsg.setText(strMessage);  
  93.         }  
  94.           
  95.         return customProgressDialog;  
  96.     }  
  97. }  
/**************************************************************************************
* [Project]
*       MyProgressDialog
* [Package]
*       com.lxd.widgets
* [FileName]
*       CustomProgressDialog.java
* [Copyright]
*       Copyright 2012 LXD All Rights Reserved.
* [History]
*       Version          Date              Author                        Record
*--------------------------------------------------------------------------------------
*       1.0.0           2012-4-27         lxd (rohsuton@gmail.com)        Create
**************************************************************************************/
	
package com.lxd.widgets;


import com.lxd.activity.R;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;


/********************************************************************
 * [Summary]
 *       TODO 请在此处简要描述此类所实现的功能。因为这项注释主要是为了在IDE环境中生成tip帮助,务必简明扼要
 * [Remarks]
 *       TODO 请在此处详细描述类的功能、调用方法、注意事项、以及与其它类的关系.
 *******************************************************************/

public class CustomProgressDialog extends Dialog {
	private Context context = null;
	private static CustomProgressDialog customProgressDialog = null;
	
	public CustomProgressDialog(Context context){
		super(context);
		this.context = context;
	}
	
	public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }
	
	public static CustomProgressDialog createDialog(Context context){
		customProgressDialog = new CustomProgressDialog(context,R.style.CustomProgressDialog);
		customProgressDialog.setContentView(R.layout.customprogressdialog);
		customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
		
		return customProgressDialog;
	}
 
    public void onWindowFocusChanged(boolean hasFocus){
    	
    	if (customProgressDialog == null){
    		return;
    	}
    	
        ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
    }
 
    /**
     * 
     * [Summary]
     *       setTitile 标题
     * @param strTitle
     * @return
     *
     */
    public CustomProgressDialog setTitile(String strTitle){
    	return customProgressDialog;
    }
    
    /**
     * 
     * [Summary]
     *       setMessage 提示内容
     * @param strMessage
     * @return
     *
     */
    public CustomProgressDialog setMessage(String strMessage){
    	TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg);
    	
    	if (tvMsg != null){
    		tvMsg.setText(strMessage);
    	}
    	
    	return customProgressDialog;
    }
}

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

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

import com.lxd.widgets.CustomProgressDialog;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MainFrame extends Activity {
	private MainFrameTask mMainFrameTask = null;
	private CustomProgressDialog progressDialog = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.main);
        
        mMainFrameTask = new MainFrameTask(this);
        mMainFrameTask.execute();
    }
    
	@Override
	protected void onDestroy() {
		stopProgressDialog();
		
		if (mMainFrameTask != null && !mMainFrameTask.isCancelled()){
			mMainFrameTask.cancel(true);
		}
		
		super.onDestroy();
	}

	private void startProgressDialog(){
		if (progressDialog == null){
			progressDialog = CustomProgressDialog.createDialog(this);
	    	progressDialog.setMessage("正在加载中...");
		}
		
    	progressDialog.show();
	}
	
	private void stopProgressDialog(){
		if (progressDialog != null){
			progressDialog.dismiss();
			progressDialog = null;
		}
	}
	
	public class MainFrameTask extends AsyncTask<Integer, String, Integer>{
		private MainFrame mainFrame = null;
		
		public MainFrameTask(MainFrame mainFrame){
			this.mainFrame = mainFrame;
		}
		
		@Override
		protected void onCancelled() {
			stopProgressDialog();
			super.onCancelled();
		}

		@Override
		protected Integer doInBackground(Integer... params) {
			
			try {
				Thread.sleep(10 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}
			
		@Override
		protected void onPreExecute() {
			startProgressDialog();
		}

		@Override
		protected void onPostExecute(Integer result) {
			stopProgressDialog();
		}
			
		
		
	}
}

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

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

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值