自定义ProgressDialog

来源:互联网 发布:华为云计算岗位及年薪 编辑:程序博客网 时间:2024/05/29 03:55

CustomProgressDialog.java

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;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.custom_progress_dialog);        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();    }    public CustomProgressDialog setTitile(String strTitle) {        return customProgressDialog;    }    public CustomProgressDialog setMessage(String strMessage) {        TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.id_tv_loadingmsg);        if (tvMsg != null) {            tvMsg.setText(strMessage);        }        return customProgressDialog;    }}

custom_progress_dialog.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="@drawable/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>

style.customDialog

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

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="@mipmap/progress1" android:duration="200"/>    <item android:drawable="@mipmap/progress2" android:duration="200"/>    <item android:drawable="@mipmap/progress3" android:duration="200"/></animation-list>

Usage

private CustomProgressDialog progressDialog;progressDialog = CustomProgressDialog.createDialog(context);progressDialog.show();if (progressDialog != null) {    progressDialog.dismiss();    progressDialog = null;}