转载请注明出处:https://blog.youkuaiyun.com/a512337862/article/details/80612225
因为项目需要,所以用自定义View实现了带进度条的下载按钮。效果图如下:
思路
主要是分为三个阶段:未下载,正在下载,下载结束(成功/失败)。根据不同的进度来判断不同的状态,从而绘制不同的UI。
代码
attr.xml
<declare-styleable name="DownloadProgressButton">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="progressColor" format="color" />
<attr name="bottomColor" format="color" />
<attr name="topColor" format="color" />
<attr name="progressWidthPercent" format="integer" />
</declare-styleable>
DownloadProgressButton.java
/**
* Author : BlackHao
* Time : 2018/5/18 10:25
* Description : 自定义带圆形进度条下载控件
* Remarks :
*/
public class DownloadProgressButton extends View implements View.OnClickListener {
/**
* 未下载
*/
public static final int READY_TO_DOWNLOAD = 0;
/**
* 正在下载
*/
public static final int IS_DOWNLOADING = 1;
/**
* 下载完成
*/
public static final int FINISH_DOWNLOAD = 2;
/**
* 下载失败
*/
public static final int FAIL_DOWNLOAD = 3;
//进度最大值
private int max;
//当前进度
private int progress;
//进度条的颜色
private int progressColor;
//进度条宽度百分比
private int progressWidthPercent;
//底层背景色
private int bottomColor;
//最上层圆的颜色
private int topColor;
//进度文本的颜色
private int textColor;
//进度文本的字体大小
private int textSize;
//进度条画笔
private Paint progressPaint;
//文本画笔
private Paint textPaint;