自定义进度条弹框

添加百分比布局依赖

 implementation 'com.zhy:percent-support-extends:1.1.1'

value文件夹添加

颜色值

 <color name="transparent">#00000000</color><!--透明-->
 <color name="gary">#e8eaef</color><!---->
 <color name="white">#FFFFFF</color><!---->
 <color name="black">#000000</color><!---->
 <color name="deep_blue">#606efd</color><!--深蓝-->
 <color name="light_blue">#009AF3</color><!--浅蓝-->
 <color name="line_grey">#dee0e8</color><!--边框灰-->

dimens文件

<!--标题-->
<dimen name="tip_size">19dip</dimen>
<!--文本-->
<dimen name="text_size">14dip</dimen>

string文件

 <string name="cancel">取消</string>
 <string name="sure">确认</string>
 <string name="loading">正在加载</string>
 <string name="progress_show">%s/%s</string>

styles文件

<style name="sp_dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item> <!-- 边框 -->
        <item name="android:windowIsFloating">true</item> <!-- 是否浮现在activity之上 -->
        <item name="android:windowNoTitle">true</item> <!-- 无标题 -->
        <item name="android:windowBackground">@android:color/transparent</item> <!-- 背景透明 -->
        <item name="android:backgroundDimEnabled">true</item> <!-- 模糊 -->
</style>

drawable文件夹添加

progress_background

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

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient android:startColor="@color/deep_blue"
                android:centerColor="@color/deep_blue" android:centerY="0.75"
                android:endColor="@color/deep_blue" android:angle="270" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient android:startColor="@color/light_blue"
                    android:centerColor="@color/light_blue" android:centerY="0.75"
                    android:endColor="@color/light_blue" android:angle="270" />
            </shape>
        </clip>
    </item>

</layer-list>

progress_dialog_background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp" />
    <solid android:color="@color/white" />
    <stroke android:color="@color/gary" android:width="1dp"/>
</shape>

progress_dialog_head_background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp" />
    <solid android:color="@color/white" />
</shape>

progress_dialog_left_button_background

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="-2dp"
        android:left="-2dp"
        android:right="-2dp">
        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="20dp" />

            <padding
                android:bottom="4dp"
                android:left="12dp"
                android:right="12dp"
                android:top="4dp" />

            <solid android:color="@color/white" />
            <stroke
                android:width="1dp"
                android:color="@color/line_grey" />
        </shape>
    </item>
</layer-list>

progress_dialog_right_button_background

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

    <solid android:color="@color/deep_blue" />

    <corners android:bottomRightRadius="20dp" />

    <padding
        android:bottom="4dp"
        android:left="12dp"
        android:right="12dp"
        android:top="4dp" />

</shape>

到这里准备工作完成。
布局文件
dialog_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<com.zhy.android.percent.support.PercentLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@color/transparent"
    android:orientation="vertical">


    <com.zhy.android.percent.support.PercentLinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/progress_dialog_background"
        android:orientation="vertical"
        app:layout_widthPercent="80%">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="@drawable/progress_dialog_head_background"
            android:gravity="center_vertical"
            android:orientation="vertical"
            tools:ignore="RtlHardcoded,SpUsage">

            <TextView
                android:id="@+id/tip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:textColor="@color/black"
                android:textSize="@dimen/tip_size" />

            <ProgressBar
                android:id="@+id/progress"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:progressDrawable="@drawable/progress_background" />

            <TextView
                android:id="@+id/progress_num"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:textColor="@color/black"
                android:textSize="@dimen/text_size"  />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:visibility="visible">

            <Button
                android:id="@+id/left"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:background="@drawable/progress_dialog_left_button_background"
                android:textColor="@color/deep_blue"
                app:layout_heightPercent="100%" />

            <Button
                android:id="@+id/right"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:background="@drawable/progress_dialog_right_button_background"
                android:textColor="@color/white"
                app:layout_heightPercent="100%" />

        </LinearLayout>

    </com.zhy.android.percent.support.PercentLinearLayout>

</com.zhy.android.percent.support.PercentLinearLayout>

ProgressDialog

public class ProgressDialog {

    private TextView tip;
    private ProgressBar progress;
    private TextView progressNum;
    private Button left;
    private Button right;
    private LinearLayout bottom;

    private Dialog dialog;
    private Context context;

    /**
     * 初始化
     *
     * @param context 上下文
     */
    public void create(Context context) {
        this.context = context;
        dialog = new Dialog(context, R.style.sp_dialog);
        dialog.setContentView(R.layout.dialog_progress);
        dialog.setCancelable(false);
        initView();
        tipUpdate(context.getString(R.string.loading), context.getString(R.string.cancel), context.getString(R.string.sure));
    }

    /**
     * 提示更新
     */
    public void tipUpdate(String tip, String leftText, String rightText) {
        if (!tip.isEmpty()) {
            this.tip.setText(tip);
        }
        if (!leftText.isEmpty()) {
            left.setText(leftText);
        }
        if (!rightText.isEmpty()) {
            right.setText(rightText);
        }
    }

    /**
     * 控件初始化
     */
    private void initView() {
        tip = dialog.findViewById(R.id.tip);
        progress = dialog.findViewById(R.id.progress);
        progressNum = dialog.findViewById(R.id.progress_num);
        left = dialog.findViewById(R.id.left);
        right = dialog.findViewById(R.id.right);
        bottom = dialog.findViewById(R.id.bottom);
    }

    /**
     * 进度显示
     *
     * @param maxNum      总进度
     * @param progressNum 当前进度
     */
    public void showProgress(int maxNum, int progressNum) {
        if (null != dialog && !dialog.isShowing()) {
            dialog.show();
        }
        tip.setTextColor(ContextCompat.getColor(context, R.color.black));
        bottom.setVisibility(View.GONE);
        this.progressNum.setText(String.format(context.getString(R.string.progress_show), String.valueOf(progressNum), String.valueOf(maxNum)));
        progress.setMax(maxNum);
        progress.setProgress(progressNum);
    }

    /**
     * 显示成功
     */
    public void showSuccess() {
        if (null != dialog && !dialog.isShowing()) {
            dialog.show();
        }
        bottom.setVisibility(View.GONE);
        tip.setTextColor(ContextCompat.getColor(context, R.color.deep_blue));
        new Handler().postDelayed(() -> dialog.dismiss(), 500);
    }

    @SuppressLint("SetTextI18n")
    public void showFail(ProgressCallBack callback) {
        if (null != dialog && !dialog.isShowing()) {
            dialog.show();
        }
        bottom.setVisibility(View.VISIBLE);
        tip.setTextColor(ContextCompat.getColor(context, R.color.deep_blue));
        left.setOnClickListener(view -> callback.leftClick(dialog));
        right.setOnClickListener(view -> callback.rightClick(dialog));
    }

    public interface ProgressCallBack {
        void leftClick(Dialog dialog);

        void rightClick(Dialog dialog);
    }
}

调用

  ProgressDialog progressDialog = new ProgressDialog();
  progressDialog.create(this);
  //进度
  progressDialog.showProgress(30,10);
  //失败
  progressDialog.showFail(new ProgressDialog.ProgressCallBack() {
            @Override
            public void leftClick(Dialog dialog) {
                
            }

            @Override
            public void rightClick(Dialog dialog) {

            }
        });
 //成功
 progressDialog.showSuccess();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值