Skip to content

Commit

Permalink
添加二级进度
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzhuo810 committed Dec 19, 2017
1 parent d5898a4 commit 7a234a4
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 19 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Github地址:https://github.com/zhouzhuo810/ZzHorizontalProgressBar
2.支持自定义背景颜色;
3.支持自定义背景与进度之间的内间距大小;
4.支持自定义最大值和默认进度值;
5.支持渐变颜色进度。
5.支持渐变颜色进度;
6.支持二级进度条。

**与系统控件相比的优势**:属性配置更简单、大小适配更方便。

Gradle:

```
compile 'me.zhouzhuo.zzhorizontalprogressbar:zz-horizontal-progressbar:1.0.2'
compile 'me.zhouzhuo.zzhorizontalprogressbar:zz-horizontal-progressbar:1.0.3'
```


Expand Down Expand Up @@ -84,23 +85,35 @@ Maven:

|属性|作用|类型|
| --- | ---|---|
| zpb_padding|背景与进度之间的内间距大小|dimension|
|zpb_padding|背景与进度之间的内间距大小|dimension|
|zpb_second_pb_color |二级进度背景颜色|color|
|zpb_bg_color |背景颜色|color|
|zpb_pb_color |进度颜色|color|
|zpb_max|进度最大值|int|
|zpb_progress|默认进度值|int|
|zpb_open_gradient_|是否使用渐变色|boolean|
|zpb_max |进度最大值|int|
|zpb_progress |默认进度值|int|
|zpb_second_progress |二级进度默认进度值|int|
|zpb_open_gradient|是否使用渐变色|boolean|
|zpb_gradient_from|开始渐变颜色|color|
|zpb_gradient_to|结束渐变颜色|color|

|zpb_show_second_progress|二级进度是否显示|boolean|
|zpb_open_second_gradient|二级进度是否使用渐变色|boolean|
|zpb_second_gradient_from|二级进度开始渐变颜色|color|
|zpb_second_gradient_to|二级进度结束渐变颜色|color|
|zpb_show_second_point_shape|二级进度形状(point,line)|enum|

### Fix Records

#### v1.2
#### v1.0.3

- 添加二级进度;
- 添加二级进度渐变;
- 添加二级进度形状;

#### v1.0.2

- 添加渐变色属性;

#### v1.1
#### v1.0.1

- 修复动态修改背景颜色和进度颜色无效问题;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ protected void onCreate(Bundle savedInstanceState) {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pb.setProgress(progress);
pb1.setProgress(progress);
pb1.setSecondProgress(progress);
pb2.setProgress(progress);
pb2.setSecondProgress(progress-30);
pb3.setProgress(progress);
pb4.setProgress(progress);
if (progress > 80) {
Expand Down
31 changes: 26 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@
<me.zhouzhuo.zzhorizontalprogressbar.ZzHorizontalProgressBar
android:id="@+id/pb1"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_height="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
app:zpb_padding="4dp" />
app:zpb_bg_color="#d8d8d8"
app:zpb_gradient_from="#429321"
app:zpb_gradient_to="#b4ec51"
app:zpb_max="100"
app:zpb_open_gradient="true"
app:zpb_padding="1px"
app:zpb_progress="50"
app:zpb_second_pb_color="#f5a623"
app:zpb_second_progress="100"
app:zpb_show_second_point_shape="point"
app:zpb_show_second_progress="true" />

<me.zhouzhuo.zzhorizontalprogressbar.ZzHorizontalProgressBar
android:id="@+id/pb2"
Expand All @@ -31,7 +41,19 @@
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
app:zpb_bg_color="@android:color/holo_orange_dark" />
app:zpb_bg_color="#d8d8d8"
app:zpb_gradient_from="#429321"
app:zpb_gradient_to="#b4ec51"
app:zpb_max="100"
app:zpb_open_gradient="true"
app:zpb_open_second_gradient="true"
app:zpb_padding="1px"
app:zpb_progress="50"
app:zpb_second_gradient_from="#f76b1c"
app:zpb_second_gradient_to="#fad961"
app:zpb_second_pb_color="#f5a623"
app:zpb_show_second_point_shape="line"
app:zpb_show_second_progress="true" />

<me.zhouzhuo.zzhorizontalprogressbar.ZzHorizontalProgressBar
android:id="@+id/pb3"
Expand All @@ -54,8 +76,7 @@
app:zpb_gradient_to="#b3eb50"
app:zpb_max="100"
app:zpb_open_gradient="true"
app:zpb_padding="1dp"
/>
app:zpb_padding="1dp" />

<TextView
android:id="@+id/tv_progress"
Expand Down
10 changes: 5 additions & 5 deletions zz-horizontal-progressbar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.0.2"
version = "1.0.3"

android {
resourcePrefix "zz_" //随意填写
Expand All @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
versionCode 3
versionName "1.2"
versionCode 4
versionName "1.3"
}
buildTypes {
release {
Expand Down Expand Up @@ -109,9 +109,9 @@ bintray {
issueTrackerUrl = 'https://github.com/zhouzhuo810/ZzHorizontalProgressBar/issues'
publicDownloadNumbers = true
version {
name = '1.0.2'
name = '1.0.3'
desc = 'zz-horizontal-progressbar release'
vcsTag = '1.0.2'
vcsTag = '1.0.3'
attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ public class ZzHorizontalProgressBar extends View {
private boolean openGradient;
private int gradientFrom;
private int gradientTo;
private boolean showSecondProgress;
private int secondProgress;
private int secondProgressShape;

private Paint secondProgressPaint;
private Paint secondGradientPaint;
private Paint progressPaint;
private Paint gradientPaint;
private Paint bgPaint;
private boolean openSecondGradient;
private int secondGradientFrom;
private int secondGradientTo;
private int secondProgressColor;

public ZzHorizontalProgressBar(Context context) {
super(context);
Expand Down Expand Up @@ -57,10 +66,17 @@ private void initAttrs(Context context, AttributeSet attrs) {
progress = a.getInteger(R.styleable.ZzHorizontalProgressBar_zpb_progress, 0);
bgColor = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_bg_color, 0xff3F51B5);
progressColor = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_pb_color, 0xffFF4081);
secondProgressColor = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_second_pb_color, 0xffFF4081);
padding = a.getDimensionPixelSize(R.styleable.ZzHorizontalProgressBar_zpb_padding, 2);
showSecondProgress = a.getBoolean(R.styleable.ZzHorizontalProgressBar_zpb_show_second_progress, false);
secondProgress = a.getInteger(R.styleable.ZzHorizontalProgressBar_zpb_second_progress, 0);
secondProgressShape = a.getInteger(R.styleable.ZzHorizontalProgressBar_zpb_show_second_point_shape, 0);
openGradient = a.getBoolean(R.styleable.ZzHorizontalProgressBar_zpb_open_gradient, false);
gradientFrom = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_gradient_from, 0xffFF4081);
gradientTo = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_gradient_to, 0xffFF4081);
openSecondGradient = a.getBoolean(R.styleable.ZzHorizontalProgressBar_zpb_open_second_gradient, false);
secondGradientFrom = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_second_gradient_from, 0xffFF4081);
secondGradientTo = a.getColor(R.styleable.ZzHorizontalProgressBar_zpb_second_gradient_to, 0xffFF4081);
a.recycle();
}

Expand All @@ -70,10 +86,19 @@ private void initPaths() {
progressPaint.setStyle(Paint.Style.FILL);
progressPaint.setAntiAlias(true);

secondProgressPaint = new Paint();
secondProgressPaint.setColor(secondProgressColor);
secondProgressPaint.setStyle(Paint.Style.FILL);
secondProgressPaint.setAntiAlias(true);

gradientPaint = new Paint();
gradientPaint.setStyle(Paint.Style.FILL);
gradientPaint.setAntiAlias(true);

secondGradientPaint = new Paint();
secondGradientPaint.setStyle(Paint.Style.FILL);
secondGradientPaint.setAntiAlias(true);

bgPaint = new Paint();
bgPaint.setColor(bgColor);
bgPaint.setStyle(Paint.Style.FILL);
Expand Down Expand Up @@ -131,6 +156,7 @@ private void drawProgress(Canvas canvas) {
RectF rectF = new RectF(padding, padding, padding + mDx, padding + progressHeight);
canvas.drawRoundRect(rectF, radius, radius, gradientPaint);
}

} else {
int progressWidth = width - padding * 2 - progressHeight;
float dx = progressWidth * percent;
Expand All @@ -144,6 +170,75 @@ private void drawProgress(Canvas canvas) {
canvas.drawRect(midRecf, progressPaint);
}

//draw second progress
if (showSecondProgress) {
float s_percent = 0;
if (max != 0) {
s_percent = secondProgress * 1.0f / max;
}
int s_progressHeight = getHeight() - padding * 2;
if (s_progressHeight % 2 != 0) {
s_progressHeight = s_progressHeight - 1;
}
if (openSecondGradient) {
int s_progressWidth = width - padding * 2;
float s_mDx = s_progressWidth * s_percent;

int s_colors[] = new int[2];
float s_positions[] = new float[2];
//from color
s_colors[0] = secondGradientFrom;
s_positions[0] = 0;
//to color
s_colors[1] = secondGradientTo;
s_positions[1] = 1;
LinearGradient s_shader = new LinearGradient(
padding + s_progressHeight / 2, padding, padding + s_progressHeight / 2 + s_mDx, padding + s_progressHeight,
s_colors,
s_positions,
Shader.TileMode.MIRROR);
//gradient
secondGradientPaint.setShader(s_shader);

int s_radius = width > getHeight() ? getHeight() / 2 : width / 2;
if (s_mDx < getHeight()) {
//left circle
canvas.drawCircle(padding + s_progressHeight / 2, padding + s_progressHeight / 2, s_progressHeight / 2, secondGradientPaint);
} else {
//progress line
RectF rectF = new RectF(padding, padding, padding + s_mDx, padding + s_progressHeight);
canvas.drawRoundRect(rectF, s_radius, s_radius, secondGradientPaint);
}
} else {
//no gradient
if (secondProgressShape == 0) {
//point shape
int s_progressWidth = width - padding * 2;
float s_mDx = s_progressWidth * s_percent;
//progress line
float px = padding + s_progressHeight / 2 + s_mDx;
if (px < width - padding - s_progressHeight / 2) {
canvas.drawCircle(px, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
} else {
canvas.drawCircle(px - s_progressHeight, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
}

} else {
//line shape
int s_progressWidth = width - padding * 2 - s_progressHeight;
float dx = s_progressWidth * percent;
secondProgressPaint.setColor(progressColor);
//left circle
canvas.drawCircle(padding + s_progressHeight / 2, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
//right circle
canvas.drawCircle(padding + s_progressHeight / 2 + dx, padding + s_progressHeight / 2, s_progressHeight / 2, secondProgressPaint);
//middle line
RectF midRecf = new RectF(padding + s_progressHeight / 2, padding, padding + s_progressHeight / 2 + dx, padding + s_progressHeight);
canvas.drawRect(midRecf, secondProgressPaint);
}
}
}

}

private void drawBackground(Canvas canvas) {
Expand Down Expand Up @@ -194,6 +289,33 @@ public void setProgress(int progress) {
invalidate();
}

public boolean isShowSecondProgress() {
return showSecondProgress;
}

public void setShowSecondProgress(boolean showSecondProgress) {
this.showSecondProgress = showSecondProgress;
invalidate();
}

public int getSecondProgress() {
return secondProgress;
}

public void setSecondProgress(int secondProgress) {
this.secondProgress = secondProgress;
invalidate();
}

public int getSecondProgressShape() {
return secondProgressShape;
}

public void setSecondProgressShape(int secondProgressShape) {
this.secondProgressShape = secondProgressShape;
invalidate();
}

public int getBgColor() {
return bgColor;
}
Expand All @@ -204,6 +326,43 @@ public void setBgColor(int bgColor) {
invalidate();
}

public boolean isOpenSecondGradient() {
return openSecondGradient;
}

public void setOpenSecondGradient(boolean openSecondGradient) {
this.openSecondGradient = openSecondGradient;
invalidate();
}

public int getSecondGradientFrom() {
return secondGradientFrom;
}

public void setSecondGradientFrom(int secondGradientFrom) {
this.secondGradientFrom = secondGradientFrom;
invalidate();
}

public int getSecondGradientTo() {
return secondGradientTo;
}

public void setSecondGradientTo(int secondGradientTo) {
this.secondGradientTo = secondGradientTo;
invalidate();
}

public int getSecondProgressColor() {
return secondProgressColor;
}

public void setSecondProgressColor(int secondProgressColor) {
this.secondProgressColor = secondProgressColor;
secondProgressPaint.setColor(secondProgressColor);
invalidate();
}

public int getProgressColor() {
return progressColor;
}
Expand Down
10 changes: 10 additions & 0 deletions zz-horizontal-progressbar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@
<attr name="zpb_padding" format="dimension" />
<attr name="zpb_bg_color" format="color|reference" />
<attr name="zpb_pb_color" format="color|reference" />
<attr name="zpb_second_pb_color" format="color|reference" />
<attr name="zpb_max" format="integer" />
<attr name="zpb_progress" format="integer" />
<attr name="zpb_show_second_progress" format="boolean" />
<attr name="zpb_second_progress" format="integer" />
<attr name="zpb_show_second_point_shape" format="enum">
<enum name="point" value="0"/>
<enum name="line" value="1"/>
</attr>
<attr name="zpb_open_gradient" format="boolean" />
<attr name="zpb_gradient_from" format="color|reference" />
<attr name="zpb_gradient_to" format="color|reference" />
<attr name="zpb_open_second_gradient" format="boolean" />
<attr name="zpb_second_gradient_from" format="color|reference" />
<attr name="zpb_second_gradient_to" format="color|reference" />
</declare-styleable>
</resources>

0 comments on commit 7a234a4

Please sign in to comment.