Loading系列:实现一个Loading弹窗
Posted zhangjin1120
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Loading系列:实现一个Loading弹窗相关的知识,希望对你有一定的参考价值。
上效果:
- 导入中间的转动圆条
//进度窗
implementation 'me.zhanghai.android.materialprogressbar:library:1.4.2'
- Dialog布局文件,
dialog_loading.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="96dp"
android:layout_height="96dp"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/bg_dialog_loading"
android:orientation="vertical"
android:gravity="center">
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/progress"
style="@style/LoadingProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"
android:maxHeight="30dp"
android:maxWidth="30dp"
android:minHeight="30dp"
android:minWidth="30dp" />
<TextView
android:id="@+id/tv_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="正在处理..."
android:layout_marginTop="5dp"
android:textColor="#ffffff"
/>
</LinearLayout>
bg_dialog_loading.xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"/>
<solid android:color="#99000000"/>
</shape>
- 转动条需要的style,路径:
res/values/styles.xml
<style name="LoadingProgressBar" parent="Widget.MaterialProgressBar.ProgressBar">
<item name="android:minWidth">32dp</item>
<item name="android:maxWidth">32dp</item>
<item name="android:minHeight">32dp</item>
<item name="android:maxHeight">32dp</item>
</style>
- Dialog需要的style,文件路径:
res/values/styles.xml
<!-- 弹窗 -->
<style name="AlertDialogStyle" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowFrame">@null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
</style>
- MainActivity.java调用显示逻辑:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public Dialog mDialog;
public void initLoadDialog(String content) {
if (mDialog == null) {
mDialog = getLoadingDialog(MainActivity.this, content);
mDialog.setCancelable(false);
} else {
dialogTvContent.setText(content);
}
if (!mDialog.isShowing()) {
mDialog.show();
}
}
TextView dialogTvContent;
public Dialog getLoadingDialog(Context context, String content) {
Dialog dialog = new Dialog(context, R.style.AlertDialogStyle);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
dialogTvContent = view.findViewById(R.id.tv_note);
if (TextUtils.isEmpty(content)) {
dialogTvContent.setVisibility(View.GONE);
} else {
dialogTvContent.setText(content);
}
dialog.setContentView(view, new ViewGroup.LayoutParams(DensityUtil.dp2px(context, 96), DensityUtil.dp2px(context, 96)));
return dialog;
}
public void dismissLoadDialog() {
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
}
public void testShowLoading(View view) {
initLoadDialog("请等待");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
dismissLoadDialog();
}
}, 2000);
}
}
DensityUtil.java
代码
public class DensityUtil {
/** dp转px */
public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
/** sp转px */
public static int sp2px(Context context, float spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}
/** px转dp */
public static float px2dp(Context context, float pxVal) {
final float scale = context.getResources().getDisplayMetrics().density;
return (pxVal / scale);
}
/** px转sp */
public static float px2sp(Context context, float pxVal) {
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
}
}
以上是关于Loading系列:实现一个Loading弹窗的主要内容,如果未能解决你的问题,请参考以下文章