加载中对话框LoadDialog

Posted 源于未知

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加载中对话框LoadDialog相关的知识,希望对你有一定的参考价值。

Dialog布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:background="#FFFFFF"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/ivIcon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true" />

        <TextView
            android:id="@+id/tvHint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ivIcon"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:ellipsize="end"
            android:gravity="center_horizontal"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:text="请稍后"
            android:textColor="#a9a9a9"
            android:textSize="10sp" />

    </RelativeLayout>
</RelativeLayout>

大概是这个样子



自定义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: xiongwenwei@aliyun.com
 * 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<Integer> 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<Integer> getResIdList() 
            List<Integer> 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的主要内容,如果未能解决你的问题,请参考以下文章

MFC 中如何点击一个按钮,则主窗口关闭,在重新加载打开

ASIHttpRequest或者SDWebImage给UIImageView加载图片的逻辑是什么样子的

如何在引导程序中的页面加载时打开模式对话框

CAD中,怎样将坐标三维坐标变成二维坐标

jQuery 将本地图片加载到打开的对话框中

如何将 ASPX 页面加载到 JQuery 对话框中