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