加载中对话框LoadDialog

Dialog布局文件




    

        

        

    

大概是这个样子

加载中对话框LoadDialog_第1张图片


自定义Dialog控件

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import com.xiong.kim.R;
import java.util.ArrayList;
import java.util.List;

/**
 * Dev: [email protected]
 * Time: 2017/9/21
 * Note: 加载中Dialog
 */

public class LoadDialog extends Dialog {

    private Context context;
    private final static int DURATION = 300;//帧动画时间间隔

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

    public static class Builder {

        private Context context;
        private String hint;
        private boolean isOutside;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置提示文字
         */
        public Builder setHint(String hint) {
            this.hint = hint;
            return this;
        }

        /**
         * 点击四周是否消失
         */
        public Builder setOutside(boolean isOutside) {
            this.isOutside = isOutside;
            return this;
        }

        /**
         * 创建Dialog
         */
        public LoadDialog create() {
            LoadDialog dialog = new LoadDialog(context);
            //背景透明
            Window window = dialog.getWindow();
            window.setBackgroundDrawable(null);
            //初始化控件
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
            ImageView ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
            TextView tvHint = (TextView) view.findViewById(R.id.tvHint);
            //提示文字
            if (!TextUtils.isEmpty(hint)) tvHint.setText(hint);
            //点击四周是否消失
            if (!isOutside) dialog.setCanceledOnTouchOutside(false);
            //设置帧动画
            AnimationDrawable animationDrawable = getAnimationDrawable(getResIdList());
            ivIcon.setImageDrawable(animationDrawable);
            //开始动画
            animationDrawable.start();
            //设置视图
            dialog.setContentView(view);
            return dialog;
        }

        /**
         * 获取AnimationDrawable
         */
        private AnimationDrawable getAnimationDrawable(List resIdList) {
            AnimationDrawable animationDrawable = new AnimationDrawable();
            for (int resId : resIdList) {
                Drawable drawable = ContextCompat.getDrawable(context, resId);
                animationDrawable.addFrame(drawable, DURATION);
            }
            animationDrawable.setOneShot(false);
            return animationDrawable;
        }

        /**
         * 帧动画资源图片ID集合
         */
        private List getResIdList() {
            List resIdList = new ArrayList<>();
            resIdList.add(R.drawable.icon_loaddialog_0);
            resIdList.add(R.drawable.icon_loaddialog_1);
            resIdList.add(R.drawable.icon_loaddialog_2);
            resIdList.add(R.drawable.icon_loaddialog_3);
            resIdList.add(R.drawable.icon_loaddialog_4);
            return resIdList;
        }
    }

}

使用

        LoadDialog loadDialog = new LoadDialog.Builder(this).setOutside(false).create();
        loadDialog.show();

加载中对话框LoadDialog_第2张图片


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