package com.mylibrary.utils; import android.app.Dialog; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.support.v4.content.ContextCompat; import android.view.LayoutInflater; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.mylibrary.R; import java.util.Timer; import java.util.TimerTask; /** * Created by geting on 2018/4/28. * 弹框的工具类 */ /** * 使用此工具类的注意事项: * 须重写onDestory 方法 并在该方法中调用hideDialog() 方法 */ public class DialogUtils { private static Dialog loadingDialog; public static void showProgressDialog(Context context) {
//解决连续创建的问题 if (loadingDialog!=null){ DialogUtils.hideDialog(); timer.cancel(); }
LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.progress_dialog, null);// 得到加载view LinearLayout layout = (LinearLayout) v.findViewById(R.id.progress_dialog_view);// 加载布局 TextView progressTv = (TextView) v.findViewById(R.id.progress_tv);// 提示文字 // main.xml中的ImageView ImageView spaceshipImage = (ImageView) v.findViewById(R.id.progress_img); // 加载动画 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation( context, R.anim.dialog_show_animation); // 使用ImageView显示动画 spaceshipImage.startAnimation(hyperspaceJumpAnimation); progressTv.setText("加载中...");// 设置加载信息 progressTv.setTextColor(ContextCompat.getColor(context,R.color.white)); loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog loadingDialog.setCancelable(true);// 不可以用“返回键”取消 loadingDialog.setContentView(layout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局 loadingDialog.getWindow().setDimAmount(0);//设置昏暗度为0 loadingDialog.getWindow().setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.transparent))); loadingDialog.show(); } public static void showAlertDialog(Context context,String str) { LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.alert_dialog_show, null);// 得到加载view LinearLayout layout = (LinearLayout) v.findViewById(R.id.alert_dialog_view);// 加载布局 TextView progressTv = (TextView) v.findViewById(R.id.alert_tv);// 提示文字 progressTv.setText(str);// 设置加载信息 progressTv.setTextColor(ContextCompat.getColor(context,R.color.white)); loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog loadingDialog.setCancelable(true);// 不可以用“返回键”取消 loadingDialog.setContentView(layout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局 loadingDialog.getWindow().setDimAmount(0);//设置昏暗度为0 loadingDialog.getWindow().setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.transparent))); loadingDialog.show(); //创建一个定时器--几秒之后消失 Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { if (loadingDialog != null) { loadingDialog.dismiss(); } } },2000); } public static void hideDialog() { if (loadingDialog != null) { loadingDialog.dismiss(); } } }
xml version="1.0" encoding="utf-8"?> <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="+360" android:duration="1500" android:startOffset="-1" android:repeatMode="restart" android:repeatCount="-1"/> set>
xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FF454645" /> <corners android:topLeftRadius="18px" android:topRightRadius="18px" android:bottomRightRadius="18px" android:bottomLeftRadius="18px"/> shape>
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/alert_dialog_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:minHeight="50dp" android:minWidth="100dp" android:background="@drawable/shape_progress" android:orientation="vertical" > <TextView android:id="@+id/alert_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> LinearLayout>
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/progress_dialog_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:minHeight="100dp" android:minWidth="100dp" android:background="@drawable/shape_progress" android:orientation="vertical" > <ImageView android:id="@+id/progress_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/icon_loading" /> <TextView android:id="@+id/progress_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="12dp" /> LinearLayout>
<resources> <style name="loading_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@nullitem> <item name="android:windowNoTitle">trueitem> <item name="android:windowBackground">@android:color/transparentitem> <item name="android:windowIsFloating">trueitem> <item name="android:windowContentOverlay">@nullitem> style> resources>