安卓加载中对话框

通用代码内容:

1、在res-values-styles文件中,添加样式:

<style name="MyDialog" parent="android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">#00000000</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
</style>

2、 在res-drawable文件夹下创建文件 shape_rectangle_whitesolid_10dpcorners.xml。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <corners android:radius="10dp" />

    <solid android:color="#fff" />

</shape>

俩种样式各自代码:

样式一:

样式一

Java类代码:

public class LoadingDataDialog1 extends Dialog {

    private final String TAG = "LoadingDataDialog";

    private TextView tvOne;

    private TextView tvTwo;

    private TextView tvThree;

    public LoadingDataDialog1(@NonNull Context context) {
        super(context, R.style.MyDialog);
    }

    private boolean isAnimate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading1);

        setCanceledOnTouchOutside(false);

        tvOne = findViewById(R.id.tv_dialog_loading_one);

        tvTwo = findViewById(R.id.tv_dialog_loading_two);

        tvThree = findViewById(R.id.tv_dialog_loading_three);

        startAnimate(tvOne);
        startAnimate(tvTwo);
        startAnimate(tvThree);

    }

    private void startAnimate(final View view) {
        isAnimate = true;
        final TranslateAnimation animateStart = new TranslateAnimation(
                0,                 // fromXDelta
                0,                 // toXDelta
                -20,  // fromYDelta
                20);                // toYDelta
        animateStart.setDuration(500);
        animateStart.setFillAfter(true);

        final TranslateAnimation animateEnd = new TranslateAnimation(
                0,                 // fromXDelta
                0,                 // toXDelta
                20,                 // fromYDelta
                -20); // toYDelta
        animateEnd.setDuration(500);
        animateEnd.setFillAfter(true);

        animateStart.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.e(TAG, "走到了 动画 开始");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.e(TAG, "走到了 动画 结束");
                if (isAnimate) {
                    view.startAnimation(animateEnd);
                } else {
                    animateEnd.reset();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        animateEnd.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (isAnimate) {
                    view.startAnimation(animateStart);
                } else {
                    animateStart.reset();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(animateStart);

    }

    @Override
    public void dismiss() {
        isAnimate = false;

        clearAnimate(tvOne);
        clearAnimate(tvTwo);
        clearAnimate(tvThree);

        super.dismiss();
    }

    private void clearAnimate(TextView textView) {
        Animation animation = textView.getAnimation();
        animation.cancel();
    }
}

布局文件代码 dialog_loading1.xml:

<?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:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center"
        android:background="@drawable/shape_rectangle_whitesolid_10dpcorners"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_dialog_loading_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="A"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_dialog_loading_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="B"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_dialog_loading_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="C"
            android:textSize="18sp" />

    </LinearLayout>

</LinearLayout>

 

样式二:

样式二

public class LoadingDataDialog2 extends Dialog {

    public LoadingDataDialog2(@NonNull Context context) {
        super(context, R.style.MyDialog);
    }

    private String title = "请求中";

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);
        TextView textView = findViewById(R.id.tv_dialog_loading_data);
        textView.setText(title);
        setCanceledOnTouchOutside(false);
    }
}

布局文件dialog_loading.xml:

<?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:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center"
        android:background="@drawable/shape_rectangle_whitesolid_10dpcorners"
        android:orientation="vertical">

        <ProgressBar
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:indeterminateBehavior="repeat" />

        <TextView
            android:id="@+id/tv_dialog_loading_data"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp"
            android:text="请求中"
            android:textSize="16sp" />

    </LinearLayout>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值