自定义progressdialog

Android自带的progressdialog确实有点丑,现在自己定义一个:

首先肯定要先有progressdialog的布局文件,但布局文件里ImageView又需要一个循环播放的图片集合,所以需要一个animation-list,有人说在res下建anim文件夹,然后建animation-list,但是我没找到animation-list,所以我实在drawable下建的animation-list:drawable——》new drawable resource file——》animation-list

还有最好自定义一个progressdialog的背景样式:drawable——》new drawable resource file——》shape

好了,上代码:

progressdialog的布局layout::

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="@drawable/bg_mdialog"
    android:paddingTop="10dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingBottom="10dp">
            android:id="@+id/load"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/mdialog" />
            android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Message"
        android:textColor="#FFFFFF" />

布局背景样式style:

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android">
    android:color="#ff404040"  />
    android:radius="8dp"/>
ImageView背景animation-list:

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android" >
    android:drawable="@drawable/a1"
        android:duration="60"/>
    android:drawable="@drawable/a2"
        android:duration="60"/>
    android:drawable="@drawable/a3"
        android:duration="60"/>
    android:drawable="@drawable/a4"
        android:duration="60"/>
    android:drawable="@drawable/a5"
        android:duration="60"/>
然后新建一个工具类class:MyProgressDialog继承自Dialog:

package zhidi.com.alertdialog;

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

/**
 * Created by Administrator on 2017/3/6.
 */

public class MyProgressDialog extends Dialog {
    public MyProgressDialog(Context context) {
        super(context);
    }

    public MyProgressDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected MyProgressDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }
    /**
     * 当窗口焦点改变时调用
     */
    public void onWindowFocusChanged(boolean hasFocus) {
        ImageView imageView = (ImageView) findViewById(R.id.load);
        // 获取ImageView上的动画背景
        AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();
        // 开始动画
        spinner.start();
    }

    /**
     * 给Dialog设置提示信息
     *
     * @param message
     */
    public void setMessage(CharSequence message) {
        if (message != null && message.length() > 0) {
            findViewById(R.id.message).setVisibility(View.VISIBLE);
            TextView txt = (TextView) findViewById(R.id.message);
            txt.setText(message);
            txt.invalidate();
        }
    }
    /**
     * 弹出自定义ProgressDialog
     *
     * @param context
     *            上下文
     * @param message
     *            提示
     * @param cancelable
     *            是否按返回键取消
     * @param cancelListener
     *            按下返回键监听
     * @return
     */
    public static MyProgressDialog show(Context context, CharSequence message, boolean cancelable, OnCancelListener cancelListener) {
        MyProgressDialog dialog = new MyProgressDialog(context, R.style.progress_my);
        dialog.setTitle("");
        dialog.setContentView(R.layout.mdialog);
        if (message == null || message.length() == 0) {
            dialog.findViewById(R.id.message).setVisibility(View.GONE);
        } else {
            TextView txt = (TextView) dialog.findViewById(R.id.message);
            txt.setText(message);
        }
        // 按返回键是否取消
        dialog.setCancelable(cancelable);
        // 监听返回键处理
        dialog.setOnCancelListener(cancelListener);
        // 设置居中
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        // 设置背景层透明度
        lp.dimAmount = 0.2f;
        dialog.getWindow().setAttributes(lp);
        // dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        dialog.show();
        return dialog;
    }
}
然后就可以在activity中调用里面的方法:

 
  
MyProgressDialog mDialog=MyProgressDialog.show(this, "加载中。。。", true, new DialogInterface.OnCancelListener() {
@Override public void onCancel(DialogInterface dialog) { Toast. makeText(MainActivity. this, "hello" ,Toast. LENGTH_SHORT).show() ; }}) ;

你可能感兴趣的:(自定义progressdialog)