增加流式编程操作

在init(Context context, AttributeSet attrs, int defStyleAttr)方法中在相应的api版本中使用strem编程如下:

if (textArray == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
IntStream.rangeClosed(1, stepCount)
.forEach(value -> {
stepTextContent.add("Step " + value);
stepTextLength.add(mTextPaint.measureText("Step " + value));
});
} else {
for (int i = 1; i <= stepCount; i++) {
stepTextContent.add("Step " + i);
stepTextLength.add(mTextPaint.measureText("Step " + i));
}
}

} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
IntStream.range(0, stepCount)
.forEach(value -> {
stepTextContent.add(textArray[value].toString());
stepTextLength.add(mTextPaint.measureText(textArray[value].toString()));
});
} else {
for (int i = 0; i < stepCount; i++) {
stepTextContent.add(textArray[i].toString());
stepTextLength.add(mTextPaint.measureText(textArray[i].toString()));
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
在方法drawStepStateImg(Canvas canvas, int w, int h)中将for循环在对应api版本中改为stream操作如下:

private void drawStepStateImg(Canvas canvas, int w, int h) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
IntStream.rangeClosed(1, stepCount)
.forEach(value -> {
if (step < value) {
if (step == (value - 1)) {
canvas.drawBitmap(certification_ing_img, w * 1f * (value - 1) / (stepCount - 1) - maxImgXRadio, completeTop(h, certification_ing_img), mPaint);
} else {
canvas.drawBitmap(certification_not_finish_img, w * 1f * (value - 1) / (stepCount - 1) - certification_not_finish_img.getWidth() / 2.0f, completeTop(h, certification_not_finish_img), mPaint);
}
} else {
canvas.drawBitmap(certification_ing_img, w * 1f * (value - 1) / (stepCount - 1) - maxImgXRadio, completeTop(h, certification_ing_img), mPaint);
canvas.drawBitmap(certification_finish_img, w * 1f * (value - 1) / (stepCount - 1) - certification_finish_img.getWidth() / 2.0f, completeTop(h, certification_finish_img), mPaint);
}
});
} else {
for (int i = 1; i <= stepCount; i++) {
if (step < i) {
if (step == (i - 1)) {
canvas.drawBitmap(certification_ing_img, w * 1f * (i - 1) / (stepCount - 1) - maxImgXRadio, completeTop(h, certification_ing_img), mPaint);
} else {
canvas.drawBitmap(certification_not_finish_img, w * 1f * (i - 1) / (stepCount - 1) - certification_not_finish_img.getWidth() / 2.0f, completeTop(h, certification_not_finish_img), mPaint);
}
} else {
canvas.drawBitmap(certification_ing_img, w * 1f * (i - 1) / (stepCount - 1) - maxImgXRadio, completeTop(h, certification_ing_img), mPaint);
canvas.drawBitmap(certification_finish_img, w * 1f * (i - 1) / (stepCount - 1) - certification_finish_img.getWidth() / 2.0f, completeTop(h, certification_finish_img), mPaint);
}
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
在绘制文字方法drawStepText(int w, int h, Canvas canvas)中将for循环在对应api版本中改为stream操作如下:

private void drawStepText(int w, int h, Canvas canvas) {
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
mTextPaint.setColor(textColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
IntStream.range(0, stepTextContent.size())
.forEach(value -> canvas.drawText(stepTextContent.get(value), value * 1.0f * w / (stepCount - 1) - stepTextLength.get(value) / 2.0f, textHeight * 3 / 2.0f - fontMetrics.bottom, mTextPaint));
} else {
for (int i = 0; i < stepTextContent.size(); i++) {
canvas.drawText(stepTextContent.get(i), i * 1.0f * w / (stepCount - 1) - stepTextLength.get(i) / 2.0f, textHeight * 3 / 2.0f - fontMetrics.bottom, mTextPaint);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
在动态设置方法void setStepTextContent(@NonNull List<String> stepTextContent)中将for循环在对应api版本中改为stream操作如下:

public void setStepTextContent(@NonNull List<String> paramList) {
this.stepCount = paramList.size();
if (stepCount < 2) {
throw new IllegalArgumentException("参数长度异常,长度至少为2");
}
stepTextContent.clear();
stepTextLength.clear();
this.stepTextContent.addAll(paramList);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
IntStream.range(0, stepCount)
.forEach(value ->
stepTextLength.add(mTextPaint.measureText(stepTextContent.get(value)))
);
} else {
for (int i = 0; i < stepCount; i++) {
stepTextLength.add(mTextPaint.measureText(stepTextContent.get(i)));
}
}
postInvalidate(http://www.amjmh.com);
}

--------------------- 

猜你喜欢

转载自www.cnblogs.com/liyanyan665/p/11295957.html