Android 自定义progressDialog实现

来源:互联网 发布:云计算发展史 编辑:程序博客网 时间:2024/06/16 04:04

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

Android 自定义progressDialog实现

1.String.xml 文件,progressDialog是继承与Dialog,先设置一下progressDialog的风格,设置背景透明色。
?
1
2
3
4
5
6
7
8
9
10
11
12
<stylename="CustomDialog"parent="@android:style/Theme.Dialog"
    <itemname="android:windowFrame">@null</item
        <itemname="android:windowIsFloating">true</item
        <itemname="android:windowContentOverlay">@null</item
        <itemname="android:windowAnimationStyle">@android:style/Animation.Dialog</item
        <itemname="android:windowSoftInputMode">stateUnspecified|adjustPan</item
    </style
       
    <stylename="CustomProgressDialog"parent="@style/CustomDialog"
        <itemname="android:windowBackground">@android:color/transparent</item
        <itemname="android:windowNoTitle">true</item
    </style>
2.customprogressdialog.xml文件,定义自己的布局,由于我的需求只需要一个进度条以及一串显示的内容,所以布局比较接单
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xmlversion="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
2
3
4
5
6
7
8
9
10
11
12
13
<?xmlversion="1.0"encoding="utf-8"?> 
<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"
    <itemandroid:drawable="@drawable/progress_1"android:duration="200"/> 
    <itemandroid:drawable="@drawable/progress_2"android:duration="200"/> 
    <itemandroid:drawable="@drawable/progress_3"android:duration="200"/> 
    <itemandroid:drawable="@drawable/progress_4"android:duration="200"/> 
    <itemandroid:drawable="@drawable/progress_5"android:duration="200"/> 
    <itemandroid:drawable="@drawable/progress_6"android:duration="200"/> 
    <itemandroid:drawable="@drawable/progress_7"android:duration="200"/> 
    <itemandroid:drawable="@drawable/progress_8"android:duration="200"/> 
</animation-list>
4.CustomProgressDialog.java文件,这个是就是我们最终需要使用的progressDialog了。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**************************************************************************************
* [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<script cf-hash="f9e31" type="text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))returnt[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */</script>)        Create
**************************************************************************************/
     
packagecom.lxd.widgets;
 
 
importcom.lxd.activity.R;
 
importandroid.app.Dialog;
importandroid.content.Context;
importandroid.graphics.drawable.AnimationDrawable;
importandroid.view.Gravity;
importandroid.widget.ImageView;
importandroid.widget.TextView;
 
 
/********************************************************************
 * [Summary]
 *       TODO 请在此处简要描述此类所实现的功能。因为这项注释主要是为了在IDE环境中生成tip帮助,务必简明扼要
 * [Remarks]
 *       TODO 请在此处详细描述类的功能、调用方法、注意事项、以及与其它类的关系.
 *******************************************************************/
 
publicclass CustomProgressDialog extendsDialog {
    privateContext context = null;
    privatestatic CustomProgressDialog customProgressDialog = null;
     
    publicCustomProgressDialog(Context context){
        super(context);
        this.context = context;
    }
     
    publicCustomProgressDialog(Context context, inttheme) {
        super(context, theme);
    }
     
    publicstatic CustomProgressDialog createDialog(Context context){
        customProgressDialog = newCustomProgressDialog(context,R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.customprogressdialog);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
         
        returncustomProgressDialog;
    }
  
    publicvoid onWindowFocusChanged(booleanhasFocus){
         
        if(customProgressDialog == null){
            return;
        }
         
        ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
    }
  
    /**
     *
     * [Summary]
     *       setTitile 标题
     * @param strTitle
     * @return
     *
     */
    publicCustomProgressDialog setTitile(String strTitle){
        returncustomProgressDialog;
    }
     
    /**
     *
     * [Summary]
     *       setMessage 提示内容
     * @param strMessage
     * @return
     *
     */
    publicCustomProgressDialog setMessage(String strMessage){
        TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg);
         
        if(tvMsg != null){
            tvMsg.setText(strMessage);
        }
         
        returncustomProgressDialog;
    }
}
5.接下来就是写一个测试activity调用我们的progressDialog了。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
packagecom.lxd.activity;
 
importcom.lxd.widgets.CustomProgressDialog;
 
importandroid.app.Activity;
importandroid.os.AsyncTask;
importandroid.os.Bundle;
importandroid.view.Window;
importandroid.view.WindowManager;
 
publicclass MainFrame extendsActivity {
    privateMainFrameTask mMainFrameTask = null;
    privateCustomProgressDialog progressDialog = null;
     
    @Override
    publicvoid 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 = newMainFrameTask(this);
        mMainFrameTask.execute();
    }
     
    @Override
    protectedvoid onDestroy() {
        stopProgressDialog();
         
        if(mMainFrameTask != null&& !mMainFrameTask.isCancelled()){
            mMainFrameTask.cancel(true);
        }
         
        super.onDestroy();
    }
 
    privatevoid startProgressDialog(){
        if(progressDialog == null){
            progressDialog = CustomProgressDialog.createDialog(this);
            progressDialog.setMessage("正在加载中...");
        }
         
        progressDialog.show();
    }
     
    privatevoid stopProgressDialog(){
        if(progressDialog != null){
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
     
    publicclass MainFrameTask extendsAsyncTask<Integer, String, Integer>{
        privateMainFrame mainFrame = null;
         
        publicMainFrameTask(MainFrame mainFrame){
            this.mainFrame = mainFrame;
        }
         
        @Override
        protectedvoid onCancelled() {
            stopProgressDialog();
            super.onCancelled();
        }
 
        @Override
        protectedInteger doInBackground(Integer... params) {
             
            try{
                Thread.sleep(101000);
            catch(InterruptedException e) {
                e.printStackTrace();
            }
            returnnull;
        }
             
        @Override
        protectedvoid onPreExecute() {
            startProgressDialog();
        }
 
        @Override
        protectedvoid onPostExecute(Integer result) {
            stopProgressDialog();
        }
             
         
         
    }
}
这样我们需要的progressDialog效果就出来了
2 0
原创粉丝点击