自定义进度Button

 这个里面包含倒计时器的使用,还有自定义进度Button,效果如下所示:

import android.os.CountDownTimer;

/**
 * Created by WJY.
 * Date: 2021-09-01
 * Time: 15:34
 * Description:
 */
public class VibrationToAudioActivity extends Activity {

    private ProgressButton btnProgress;//带进度的button

    //倒计时器
    private VerificationCodeCountDownTimer mTimer;
    //延迟时间
    private final static int TOTAL_TIME = 100 * 1000;

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

        initView();
    }

    private void initView() {
        btnProgress = findViewById(R.id.button_progress_green);
        btnProgress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startCountDown();
                mTimer = new VerificationCodeCountDownTimer(TOTAL_TIME, 2000);
                //开始倒计时
                mTimer.start();
            }
        });
    }

    //时间倒计时
    private class VerificationCodeCountDownTimer extends CountDownTimer {

        public VerificationCodeCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            countDown((int) (l / 1000));
        }

        @Override
        public void onFinish() {
            refreshCountDown();
        }
    }

    @SuppressLint("StringFormatMatches")
    private void countDown(int remaining) {
        btnProgress.setText(getString(R.string.retrieve_password_recapture_code_times, 100 - remaining));
        btnProgress.setTextColor(getResources().getColor(R.color.color_light_blue));
        btnProgress.setProgress(100 - remaining);
    }

    private void refreshCountDown() {
        btnProgress.setText(R.string.retrieve_password_recapture_code);
        btnProgress.setTextColor(Color.WHITE);
        btnProgress.setEnabled(true);
    }

    private void startCountDown() {
        btnProgress.setEnabled(false);
        btnProgress.reset();
    }
}

带进度条的Button按钮

package com.example.test1.customView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatButton;

import com.example.test1.R;

/**
 * Created by WJY.
 * Date: 2021-09-15
 * Time: 9:53
 * Description: 带进度条的按钮
 */
public class ProgressButton extends AppCompatButton {

    private float mCornerRadius = 0;
    private float mProgressMargin = 0;

    private boolean mFinish;

    private int mProgress;
    private int mMaxProgress = 100;
    private int mMinProgress = 0;

    private GradientDrawable mDrawableButton;
    private GradientDrawable mDrawableProgressBackground;
    private GradientDrawable mDrawableProgress;

    public ProgressButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public ProgressButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize(context, attrs);
    }

    private void initialize(Context context, AttributeSet attrs) {
        //Progress background drawable
        mDrawableProgressBackground = new GradientDrawable();
        //Progress drawable
        mDrawableProgress = new GradientDrawable();
        //Normal drawable
        mDrawableButton = new GradientDrawable();

        //Get default normal color
        int defaultButtonColor = getResources().getColor(R.color.notice_process, null);
        //Get default progress color
        int defaultProgressColor = getResources().getColor(R.color.yellow, null);
        //Get default progress background color
        int defaultBackColor = getResources().getColor(R.color.aa, null);

        TypedArray attr = context.obtainStyledAttributes(attrs, R.styleable.ProgressButton);

        try {
            mProgressMargin = attr.getDimension(R.styleable.ProgressButton_progressMargin, mProgressMargin);
            mCornerRadius = attr.getDimension(R.styleable.ProgressButton_cornerRadius, mCornerRadius);
            //Get custom normal color
            int buttonColor = attr.getColor(R.styleable.ProgressButton_buttonColor, defaultButtonColor);
            //Set normal color
            mDrawableButton.setColor(buttonColor);
            //Get custom progress background color
            int progressBackColor = attr.getColor(R.styleable.ProgressButton_progressBackColor, defaultBackColor);
            //Set progress background drawable color
            mDrawableProgressBackground.setColor(progressBackColor);
            //Get custom progress color
            int progressColor = attr.getColor(R.styleable.ProgressButton_progressColor, defaultProgressColor);
            //Set progress drawable color
            mDrawableProgress.setColor(progressColor);

            //Get default progress
            mProgress = attr.getInteger(R.styleable.ProgressButton_progress, mProgress);
            //Get minimum progress
            mMinProgress = attr.getInteger(R.styleable.ProgressButton_minProgress, mMinProgress);
            //Get maximize progress
            mMaxProgress = attr.getInteger(R.styleable.ProgressButton_maxProgress, mMaxProgress);

        } finally {
            attr.recycle();
        }

        //Set corner radius
        mDrawableButton.setCornerRadius(mCornerRadius);
        mDrawableProgressBackground.setCornerRadius(mCornerRadius);
        mDrawableProgress.setCornerRadius(mCornerRadius - mProgressMargin);
        setBackgroundDrawable(mDrawableButton);

        mFinish = false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mProgress > mMinProgress && mProgress <= mMaxProgress && !mFinish) {
            //Calculate the width of progress
            float progressWidth = (float) getMeasuredWidth() * ((float) (mProgress - mMinProgress) / mMaxProgress - mMinProgress);
            //If progress width less than 2x corner radius, the radius of progress will be wrong
            if (progressWidth < mCornerRadius * 2) {
                progressWidth = mCornerRadius * 2;
            }

            //Set rect of progress
            mDrawableProgress.setBounds((int) mProgressMargin, (int) mProgressMargin,
                    (int) (progressWidth - mProgressMargin), getMeasuredHeight() - (int) mProgressMargin);

            //Draw progress
            mDrawableProgress.draw(canvas);

            if (mProgress == mMaxProgress) {
                setBackgroundDrawable(mDrawableButton);
                mFinish = true;
            }
        }
        super.onDraw(canvas);
    }

    /**
     * Set current progress
     */
    public void setProgress(int progress) {
        if (!mFinish) {
            mProgress = progress;
            setBackgroundDrawable(mDrawableProgressBackground);
            invalidate();
        }
    }

    public void setMaxProgress(int maxProgress) {
        mMaxProgress = maxProgress;
    }

    public void setMinProgress(int minProgress) {
        mMinProgress = minProgress;
    }

    public void reset() {
        mFinish = false;
        mProgress = mMinProgress;
    }
}

attr.xml文件里设置自定义Button的属性

    <!-- 带进度条的button -->
    <declare-styleable name="ProgressButton">
        <attr name="progressColor" />
        <attr name="progressBackColor" />
        <attr name="buttonColor" format="color" />
        <attr name="cornerRadius" format="dimension" />
        <attr name="progress" />
        <attr name="minProgress" />
        <attr name="maxProgress" />
        <attr name="progressMargin" format="dimension" />
    </declare-styleable>

设置文字

    <string name="retrieve_password_recapture_code_times">%1$s%%</string>
    <string name="retrieve_password_recapture_code">重新获取</string>

布局使用

<com.example.test1.customView.ProgressButton
        android:id="@+id/button_progress_green"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_40dp"
        android:layout_marginTop="6dp"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:text="进度"
        app:progress="10"
        app:cornerRadius="8dp"
        app:progressMargin="2dp"
        app:progressColor="@color/text_work_wait"
        app:buttonColor="@color/aa" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代新人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值