LoadingDialog 加載框集成

前言

將等待加載框進行集成,可以根據不同的需求,顯示不同的等待加載框。Github地址:https://github.com/lzy2626/LzyLoading

效果

使 用

To get a Git project into your build:

Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

Step 2. Add the dependency

    dependencies {
            implementation 'com.github.lzy2626:LzyLoading:1.9'
    }

Step3. 代碼調用

1.使用系統默認圖,可以修改顏色

       new LoadingDialog.Builder(MainActivity.this)
                .msg("加載中...")
                .color(R.color.colorPrimary)//修改顏色
                .build()
                .show();

2.自定義圖片

       new LoadingDialog.Builder(MainActivity.this)
                .msg("加載中...")
                .image(R.drawable.loading_dialog_progressbar)
                .build()
                .show();

loading_dialog_progressbar寫法:

        
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@mipmap/bga_refresh_loading01"
    android:pivotX="50%"
    android:pivotY="50%" />

3.gif圖

       new LoadingDialog.Builder(MainActivity.this)
                .msg("加載中...")
                .gifImage(R.mipmap.num86)
                .build()
                .show();

實現

1.核心代碼

  /**
     * 三種加載樣式:1.系統默認圖,可設置顏色 2.自定義圖片 3.Gif
     * 比重遞減:
     * gif有數據,自定義有數據,顯示gif
     * gif無數據,自定義有數據,顯示自定義圖片
     * gif無數據,自定義無數據,顯示系統樣式
     */
    public void show() {
        View view;
        if (gifImage != -1) {//設置了gif
            view = LayoutInflater.from(context).inflate(R.layout.dialog_loadinggif, null);
            GifImageView gifImageView = view.findViewById(R.id.gifimageview);
            gifImageView.setImageResource(gifImage);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
            ProgressBar progressBar = view.findViewById(R.id.progressbar);
            LinearLayout llProgress = view.findViewById(R.id.ll_progress);

            //設置背景色
            if (background != -1) {
                llProgress.setBackgroundColor(context.getResources().getColor(background));
            }


            if (image != -1) {//設置了image圖片,沒有設置的話,使用系統樣式
                Drawable wrapDrawable = DrawableCompat.wrap(context.getResources().getDrawable(image));
                progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
            } else {//使用系統默認圖片
                //使用系統樣式可以設置樣式顏色
                if (color != -1) {
                    //21以上處理方式和21以下版本的處理方式不同
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_IN);

                    } else {
                        Drawable wrapDrawable = DrawableCompat.wrap(progressBar.getIndeterminateDrawable());
                        DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(context, color));
                        progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
                    }
                }

            }


        }
        TextView loadingText = view.findViewById(R.id.id_tv_loading_dialog_text);
        //設置提示語
        if (TextUtils.isEmpty(msg)) {
            loadingText.setVisibility(View.GONE);
        } else {
            loadingText.setVisibility(View.VISIBLE);
            loadingText.setText(msg);
            if (msgColor != -1)
                loadingText.setTextColor(context.getResources().getColor(msgColor));
        }

        mLoadingDialog = new Dialog(context, R.style.CustomProgressDialog);
        mLoadingDialog.setCancelable(cancelable);
        mLoadingDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

        Window window = mLoadingDialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = width;
        params.width = height;
        window.setAttributes(params);

        mLoadingDialog.show();
    }

2.參數

/**
     * 提示語
     */
    private String msg;
    private int msgColor;
    /**
     * 是否可取消
     */
    private boolean cancelable;
    /**
     * 點擊dialog以外的區域是否關閉
     */
    private boolean canceledOnTouchOutside;
    /**
     * 菊花的顏色
     */
    private int color;
    /**
     * 背景色
     */
    private int background;

    /**
     * image圖片
     */
    private int image;
    /**
     * gif圖片
     */
    private int gifImage;
    /**
     *寬高
     */
    private int width;
    private int height;
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。