android中自定義progressDialog實現

jopen 11年前發布 | 23K 次閱讀 Android開發 移動開發 Android

我們在項目中經常會遇到這樣一個應用場景:執行某個耗時操作時,為了安撫用戶等待的煩躁心情我們一般會使用進度條之類的空間,在android中讓大家最 容易想到的就是progressbar或者progressDialog,區別在于前者是一個控件,后者是對話框。由于一些需求在彈出進度條時不希望用戶 能夠操作其他控件,所以只能使用progressDialog,這個時候有遇到了一個問題,我不想要progressDialog的黑色框框,感覺這樣跟 應用的整體風格不協調,這個時候就考慮了寫一個自定義的progressDialog。
         在網上搜過很多自定義progressDialog的例子,對著寫了下,但是沒有任何效果,不知道是自己使用的方法不對還是什么地方出錯了。通過不斷的查找資料,寫了一個簡單的自定義progressDialog。先上圖看下效果:

android中自定義progressDialog實現

1.String.xml 文件,progressDialog是繼承與Dialog,先設置一下progressDialog的風格,設置背景透明色。

<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文件,定義自己的布局,由于我的需求只需要一個進度條以及一串顯示的內容,所以布局比較接單

<?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  
<?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>         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文件.這個文件為了實現轉動的效果,循環顯示這些圖片。

<?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了

 

/**************************************************************************************
* [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了。

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();
        }



    }
}

來自:http://blog.csdn.net/a468903507/article/details/8889278

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!