安卓开发之自定义带加载进度的按钮ProgressButton

本文介绍如何自定义一个带有加载进度的按钮ProgressButton,包括定义自定义属性如进度条颜色、背景颜色,以及在MainActivity中的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

demo按钮效果图:

这里写图片描述

一、定义ProgressButton的自定义属性

在attrs文件中定义ProgressButton的基本属性:主要有进度条颜色、进度条的背景颜色、按钮正常和被点击状态时的颜色、按钮的边角半径和是否显示进度信息。

<declare-styleable name="progressbutton">
    <attr name="progressColor" format="color" />
    <attr name="progressBgColor" format="color" />
    <attr name="buttonNormalColor" format="color" />
    <attr name="buttonPressedColor" format="color" />
    <attr name="buttonCornerRadius" format="dimension"/>
    <attr name="showProgressNum" format="boolean"/>
</declare-styleable>

二、继承Button自定义ProgressButton

package com.cxmscb.cxm.progressbutton;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.Button;

/**
 * Created by cxm on 2016/9/7.
 */
public class ProgressButton extends Button {


    private int mProgress; //当前进度
    private int mMaxProgress = 100; //最大进度:默认为100
    private int mMinProgress = 0;//最小进度:默认为0
    private GradientDrawable mProgressDrawable;// 加载进度时的进度颜色
    private GradientDrawable mProgressDrawableBg;// 加载进度时的背景色
    private StateListDrawable mNormalDrawable; // 按钮在不同状态的颜色效果
    private boolean isShowProgress;  //是否展示进度
    private boolean isFinish; // 结束状态
    private boolean isStop;// 停止状态
    private boolean isStart ; // 刚开始的状态
    private OnStateListener onStateListener; //结束时的监听
    private float cornerRadius; // 圆角半径



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

    public ProgressButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public Progress
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值