先看看实现效果图
1.首先自定义一个类继承系统ProgressDialog
/** * Created by hanbao0928 on 2018/11/1. */ public class DialogUtils extends ProgressDialog { public DialogUtils(Context context) { super(context); } public DialogUtils(Context context, int theme) { super(context, theme); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(getContext()); } private void init(Context context) { setCancelable(true); setCanceledOnTouchOutside(false); setContentView(R.layout.loading);//loading的xml文件 WindowManager.LayoutParams params = getWindow().getAttributes(); params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; getWindow().setAttributes(params); } @Override public void show() {//开启 super.show(); } @Override public void dismiss() {//关闭 super.dismiss(); } }
2.设置loading布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_horizontal" android:background="@drawable/shape_dialog_bg"//背景色 android:layout_centerInParent="true" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <ProgressBar android:id="@+id/pb_load" android:layout_width="65dp" android:layout_height="65dp" android:indeterminateDrawable="@drawable/progressbar"//加载圈的样式 android:layout_centerInParent="true"/> </RelativeLayout> <TextView android:id="@+id/tv_load_dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="加载中..." android:textColor="#9a9b98" android:textSize="12sp"/> </LinearLayout>
3.背景色(可自行调整)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="8dp" /> <solid android:color="#88000000" /> </shape>
4.加载圈样式(可自行调整)
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="720"> <shape android:shape="ring" android:innerRadiusRatio="3" android:thicknessRatio="15" android:useLevel="false"> <gradient android:type="sweep" android:useLevel="false" android:startColor="#55c6c6c6" android:centerColor="#c6c6c6" android:centerY="0.50" android:endColor="#c6c6c6" /> </shape> </animated-rotate>
5.原来需要在创建自定义的loading 的时候在传入 new Loading_view(this,R.style.CustomDialog);样式
<style name="CustomDialog" parent="Theme.AppCompat.Dialog"> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
6.代码中使用
public class MainActivity extends AppCompatActivity { private DialogUtils loading; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void loding(View v){//点击加载并按钮模仿网络请求 loading = new DialogUtils(this,R.style.CustomDialog); loading.show(); new Handler().postDelayed(new Runnable() {//定义延时任务模仿网络请求 @Override public void run() { loading.dismiss();//3秒后调用关闭加载的方法 } }, 1000); } }
ok 这样就实现菊花加载效果啦!!!!