Android custom loading animated Dialog popup

First look at the effect:

 Implementation steps:

1. Create a new LoadingDialog class (can be copied directly):

public class LoadingDialog extends Dialog {
    public LoadingDialog(Context context) {
        super(context, R.style.loading_dialog);

        initView();
    }

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                if(LoadingDialog.this.isShowing())
                    LoadingDialog.this.dismiss();
                break;
        }
        return true;
    }

    private void initView(){
        setContentView(R.layout.dialog_loading);
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.loading_animation);
        animation.setInterpolator(new LinearInterpolator());
        findViewById(R.id.loading_dialog_img).startAnimation(animation);
        setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha=0.8f;
        getWindow().setAttributes(attributes);
        setCancelable(false);
    }
}

2. dialog_loading layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dialog_view"
    android:background="@color/white"
    android:gravity="center"
    android:minHeight="60dp"
    android:minWidth="180dp"
    android:orientation="vertical"
    android:padding="10dp">
    <LinearLayout
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@color/white"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/loading_dialog_img"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitXY"
            android:src="@drawable/loding_icon" />

        <TextView
            android:id="@+id/tv_loading_tx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:maxLines="1"
            android:text="加载中..."
            android:textColor="@color/black"
            android:textSize="14sp" />
    </LinearLayout>
</LinearLayout>

3. loading_dialog style: (add in styles in values)

<!-- 自定义loading dialog -->
<style name="loading_dialog" parent="android:style/Theme.Dialog" >
    <item name="android:windowFrame"> @null</item>
    <item name="android:windowNoTitle"> true</item>
    <item name="android:windowBackground"> @color/white</item>
    <item name="android:windowIsFloating"> true</item>
    <item name="android:windowContentOverlay"> @null</item>
</style>

4. loading_animation (must be placed in the anim folder, if not, create one under res)

<?xml version="1.0" encoding="utf-8"?>
<set 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>

5. Call this pop-up window where needed, and close the pop-up window after loading

//全局化
private LoadingDialog dialog;

//在需要的地方调用此方法
private void loadingDialog() {
        dialog = new LoadingDialog(this);
        dialog.show();
    }

//加载完成后关闭此弹窗
dialog.dismiss();

Attach the loding_icon picture:

 

Guess you like

Origin blog.csdn.net/L73748196_/article/details/128121180