自定义透明背景ProgressDialog

通过继承Dialog+帧动画实现自定义透明背景ProgressDialog。

下载资源地址:http://download.csdn.net/download/shenyuanqing/8984687

效果动画:

自定义透明背景ProgressDialog_第1张图片      自定义透明背景ProgressDialog_第2张图片


CommProgressDialog:

package com.example.administrator.progressdialog;

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

/**
 * Created by Administrator on 2015/7/29.
 */
public class CommProgressDialog extends Dialog {
    private Context context = null;
    private int anim=0;
    private static CommProgressDialog commProgressDialog = null;

    public CommProgressDialog(Context context){
        super(context);
        this.context = context;
    }

    public CommProgressDialog(Context context, int theme, int anim) {
        super(context, theme);
        this.anim=anim;
    }

    public static CommProgressDialog createDialog(Context context,int anim){
        commProgressDialog = new CommProgressDialog(context, R.style.CommProgressDialog ,anim);
        commProgressDialog.setContentView(R.layout.comm_progress_dialog);
        commProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

        return commProgressDialog;
    }



    public void onWindowFocusChanged(boolean hasFocus){

        if (commProgressDialog == null){
            return;
        }

        ImageView imageView = (ImageView) commProgressDialog.findViewById(R.id.iv_loading);
        if(anim!=0) {
            imageView.setBackgroundResource(anim);
        }
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
    }

    /**
     * 设置标题
     * @param strTitle
     * @return
     */
    public CommProgressDialog setTitile(String strTitle){
        return commProgressDialog;
    }

    /**
     * 设置提示内容
     * @param strMessage
     * @return
     */
    public CommProgressDialog setMessage(String strMessage){
        TextView tvMsg = (TextView)commProgressDialog.findViewById(R.id.tv_loading_msg);

        if (tvMsg != null){
            tvMsg.setText(strMessage);
        }

        return commProgressDialog;
    }

    /**屏蔽返回键**/
    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK)
            return true;
        return super.onKeyDown(keyCode, event);
    }
}

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </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="CommProgressDialog" parent="@style/CustomDialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
    </style>

</resources>

comm_progress_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/iv_loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/default_anim"
        android:layout_gravity="center"/>
    <TextView
        android:id="@+id/tv_loading_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:textSize="16dp"/>
</LinearLayout>


default_anim.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/loading_1" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_2" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_3" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_4" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_5" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_6" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_7" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_8" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_9" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_10" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_11" android:duration="150"></item>
    <item android:drawable="@mipmap/loading_12" android:duration="150"></item>
</animation-list>

my_anim.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/chrysanthemum_1" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_2" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_3" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_4" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_5" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_6" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_7" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_8" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_9" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_10" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_11" android:duration="150"></item>
    <item android:drawable="@mipmap/chrysanthemum_12" android:duration="150"></item>
</animation-list>


MainActivity:

package com.example.administrator.progressdialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends Activity {
    private CommProgressDialog progressDialog=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.bt_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (progressDialog == null) {
                    progressDialog = CommProgressDialog.createDialog(MainActivity.this, R.anim.my_anim);
                }
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
        });
    }
}



你可能感兴趣的:(自定义,ProgressDialog,透明背景,帧动画)