先看看實現效果圖

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 這樣就實現菊花加載效果啦!!!!
