Skip to content

Commit

Permalink
更新:防止在输入较大int数值的时候,计算进度操作导致int类型溢出的情况,如下载场景下的进度数值。
Browse files Browse the repository at this point in the history
  • Loading branch information
totond committed Jul 8, 2017
1 parent 92c1b4c commit a004d7b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CustomProgressBar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ publish {
repoName = 'CustomViewRepository' //Repository的名字
groupId = 'com.yanzhikaijky' //包名
artifactId = 'PictureProgressbar' //项目名
publishVersion = '1.1.0' //版本号
publishVersion = '1.1.1' //版本号
desc = 'a picture progressbar' //description说明,随便写
website = 'https://github.com/totond/PictureProgressBar'//VCS地址,这里最好写GitHub的,我试过不写然后上传不了
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class PictureProgressBar extends View {
private int roundX = 20, roundY = 20;
//进度值和最大值
private int progress = 0, max = 100;
//进度条百分比
private float progressPercentage = 0;
//进度条当前进度中心点
private int x, y;
//是否令设进度条宽高
Expand Down Expand Up @@ -148,7 +150,7 @@ public boolean onPreDraw() {
@Override
protected synchronized void onDraw(Canvas canvas) {
//获取进度条当前进度的中心点坐标
x = (progressWidth - halfDrawableWidth) * progress / max + halfDrawableWidth;
x = (int) ((progressWidth - halfDrawableWidth) * progressPercentage + halfDrawableWidth);
y = getHeight() / 2;

drawBar(canvas);
Expand Down Expand Up @@ -273,8 +275,9 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(width, height);
}


//设置进度
public void setProgress(int progress) {
public synchronized void setProgress(int progress) {
if (progress <= max) {
this.progress = progress;
} else if (progress < 0){
Expand All @@ -283,6 +286,7 @@ public void setProgress(int progress) {
else {
this.progress = max;
}
progressPercentage = progress / max;
doProgressRefresh();
}

Expand Down Expand Up @@ -487,7 +491,7 @@ public void setOnProgressChangeListener(OnProgressChangeListener onProgressChang
public interface OnProgressChangeListener {
//进度改变时的回调
public void onOnProgressChange(int progress);
//进度完成时的回答
//进度完成时的回调
public void onOnProgressFinish();
}
}
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,26 @@
```
## 开源协议
  PictureProgressBar遵循Apache 2.0开源协议。
## 后续
  目前没有加入文字,主要是想不到在哪个位置加比较好。后续看看需求加入文字和一些其它的动画,目前在学习RxJava,看看用观察者模式来实现会不会更好,貌似Android源码的ProgressBar就是基于观察者模式来刷新进度条的,可以继承它来实现试试,不同于我这种定时刷新,减少了资源的消耗,但是这样的话动画的处理会是个难点。

## 更新
- 2017/07/07更新:
  加入了progressPercentage属性来表示进度条进度比例,修改了`setProgress()`内容,在里面加入

```
progressPercentage = progress/max;
```
  防止在输入较大int数值的时候,计算进度操作导致int类型溢出的情况,如下载场景下的进度数值。

## 关于作者
id:炎之铠
炎之铠的邮箱:[email protected]
CSDN:http://blog.csdn.net/totond
PictureProgressBar全解析blog:
http://blog.csdn.net/totond/article/details/72359888
http://www.jianshu.com/p/009cb305ec5f
> id:炎之铠
> 炎之铠的邮箱:[email protected]
> CSDN:http://blog.csdn.net/totond
> PictureProgressBar全解析blog:
> http://blog.csdn.net/totond/article/details/72359888
> http://www.jianshu.com/p/009cb305ec5f
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Tue Jul 04 16:54:40 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

0 comments on commit a004d7b

Please sign in to comment.