前言
將等待加載框進(jìn)行集成住涉,可以根據(jù)不同的需求木人,顯示不同的等待加載框巾兆。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. 代碼調(diào)用
1.使用系統(tǒng)默認(rèn)圖,可以修改顏色
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寫(xiě)法:
<?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();
實(shí)現(xiàn)
1.核心代碼
/**
* 三種加載樣式:1.系統(tǒng)默認(rèn)圖虎囚,可設(shè)置顏色 2.自定義圖片 3.Gif
* 比重遞減:
* gif有數(shù)據(jù),自定義有數(shù)據(jù)蔫磨,顯示gif
* gif無(wú)數(shù)據(jù)淘讥,自定義有數(shù)據(jù),顯示自定義圖片
* gif無(wú)數(shù)據(jù)堤如,自定義無(wú)數(shù)據(jù)蒲列,顯示系統(tǒng)樣式
*/
public void show() {
View view;
if (gifImage != -1) {//設(shè)置了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);
//設(shè)置背景色
if (background != -1) {
llProgress.setBackgroundColor(context.getResources().getColor(background));
}
if (image != -1) {//設(shè)置了image圖片,沒(méi)有設(shè)置的話搀罢,使用系統(tǒng)樣式
Drawable wrapDrawable = DrawableCompat.wrap(context.getResources().getDrawable(image));
progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
} else {//使用系統(tǒng)默認(rèn)圖片
//使用系統(tǒng)樣式可以設(shè)置樣式顏色
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);
//設(shè)置提示語(yǔ)
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.參數(shù)
/**
* 提示語(yǔ)
*/
private String msg;
private int msgColor;
/**
* 是否可取消
*/
private boolean cancelable;
/**
* 點(diǎn)擊dialog以外的區(qū)域是否關(guān)閉
*/
private boolean canceledOnTouchOutside;
/**
* 菊花的顏色
*/
private int color;
/**
* 背景色
*/
private int background;
/**
* image圖片
*/
private int image;
/**
* gif圖片
*/
private int gifImage;
/**
*寬高
*/
private int width;
private int height;