加载对话框(LoadingDialog)

加载对话框(LoadingDialog)

一、使用方法

  • 在BaseActivity中添加以下代码:
private LoadingDialog loadingDialog;
public LoadingDialog getLoadingDialog() {
    if(loadingDialog == null){
        loadingDialog = new LoadingDialog(this);
    }
    return loadingDialog;
}
  • 在BaseActivity子类中使用:
显示:getLoadingDialog().show("提示文字",true); 
关闭:getLoadingDialog().dismiss();
//true or false 表示是否可以点空白区取消

二、代码目录

LoadingDialog.java 类
dialog_loading.xml 布局
loading_dialog_anim.xml 动画
shape_loading_dialog.xml 形状
styles.xml 样式标签
图片资源

三、代码与资源

  • LoadingDialog.java 加载对话框类
public class LoadingDialog extends Dialog {

    public LoadingDialog(Context context) {
        super(context, R.style.Loading_Dialog);
        setContentView(R.layout.dialog_loading);
    }

    public void show(String message,boolean cancelable){
        show(message,cancelable,null);
    }

    public void show(String message,boolean cancelable,OnCancelListener cancelListener){
        TextView tv= (TextView)findViewById(R.id.message);
        //设置显示文本
        if(message == null || message.length() == 0){
            tv.setVisibility(View.GONE);
        }else{
            tv.setText(message);
        }
        //设置返回键是否取消
        this.setCancelable(cancelable);
        //设置取消键的监听
        this.setOnCancelListener(cancelListener);
        // 设置居中
        this.getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        // 设置背景层透明度
        lp.dimAmount = 0.2f;
        this.getWindow().setAttributes(lp);
        //显示
        try {
            this.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**关闭*/
    public void dismiss(){
        try {
            super.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**设置动画,当窗口焦点改变时调用*/
    public void onWindowFocusChanged(boolean hasFocus) {
        ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
        // 获取ImageView上的动画背景
        AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();
        // 开始动画
        spinner.start();
    }

}
  • dialog_loading.xml 加载对话框布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/shape_loading_dialog"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="20dp">

    <ImageView
        android:id="@+id/spinnerImageView"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@anim/loading_dialog_anim" />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Message"
        android:textColor="#FFFFFF" />

LinearLayout>
  • loading_dialog_anim.xml 加载对话框动画
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <item
        android:drawable="@drawable/loading_0"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_1"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_2"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_3"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_4"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_5"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_6"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_7"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_8"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_9"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_10"
        android:duration="60" />
    <item
        android:drawable="@drawable/loading_11"
        android:duration="60" />
animation-list>
  • shape_loading_dialog.xml 对话框形状

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#ff404040" />

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

shape>
  • styles.xml中 添加以下样式

<style name="Loading_Dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null
    "android:windowIsFloating">true
    "android:windowContentOverlay">@null
    "android:windowAnimationStyle">@android:style/Animation.Dialog
    "android:windowSoftInputMode">stateUnspecified|adjustPan
    "android:windowBackground">@android:color/transparent
    "android:windowNoTitle">true
style>
  • 图片资源
    图片资源LoadingDialog.rar

– the end –

你可能感兴趣的:(加载对话框(LoadingDialog))