Skip to content

Commit c8c43f4

Browse files
committed
Optimize progress
1 parent 095b0dd commit c8c43f4

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
android:layout_width="match_parent"
8080
android:layout_height="wrap_content"
8181
android:layout_margin="10dp"
82-
android:progress="50"
82+
android:progress="99"
8383
android:visibility="visible"
8484
app:lyj_cut_corner="ellipse"
8585
app:lyj_reached_color="#00FBD0"
@@ -102,7 +102,7 @@
102102
android:layout_width="match_parent"
103103
android:layout_height="wrap_content"
104104
android:layout_margin="10dp"
105-
android:progress="50"
105+
android:progress="99"
106106
android:visibility="visible"
107107
app:lyj_cut_corner="ellipse"
108108
app:lyj_reached_color="#00FBD0"

library/src/main/java/com/github/gzuliyujiang/progressbar/EnhancedProgressBar.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import android.graphics.Canvas;
2020
import android.graphics.Paint;
2121
import android.graphics.Path;
22-
import android.graphics.Rect;
2322
import android.graphics.RectF;
2423
import android.graphics.Typeface;
2524
import android.util.AttributeSet;
@@ -80,6 +79,7 @@ private void init(Context context, AttributeSet attrs) {
8079
if (attrs != null) {
8180
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EnhancedProgressBar);
8281
mProgress = a.getInt(R.styleable.EnhancedProgressBar_android_progress, 0);
82+
mProgress = Math.min(mProgress, getMax());
8383
mTextColor = a.getColor(R.styleable.EnhancedProgressBar_lyj_text_color, DEFAULT_TEXT_COLOR);
8484
mTextSize = (int) a.getDimension(R.styleable.EnhancedProgressBar_lyj_text_size, mTextSize);
8585
mReachedBarColor = a.getColor(R.styleable.EnhancedProgressBar_lyj_reached_color, mTextColor);
@@ -99,6 +99,7 @@ private void init(Context context, AttributeSet attrs) {
9999
}
100100
mBarHeight = Math.max(mReachedBarHeight, mUnReachedBarHeight);
101101
mPaint.setAntiAlias(true);
102+
mPaint.setTypeface(Typeface.MONOSPACE);
102103
mPaint.setTextSize(mTextSize);
103104
mPaint.setColor(mTextColor);
104105
}
@@ -145,13 +146,13 @@ public synchronized int getProgress() {
145146

146147
@Override
147148
public synchronized void setProgress(int progress) {
148-
mProgress = progress;
149+
mProgress = Math.min(progress, getMax());
149150
invalidate();
150151
}
151152

152153
@Override
153154
public void setProgress(int progress, boolean animate) {
154-
mProgress = progress;
155+
mProgress = Math.min(progress, getMax());
155156
invalidate();
156157
}
157158

@@ -161,17 +162,16 @@ protected synchronized void onDraw(Canvas canvas) {
161162
canvas.translate(getPaddingLeft(), getHeight() / 2f);
162163
boolean needDrawUnreachedBar = true;
163164
String text = mProgress + "%";
164-
float textWidth;
165-
if (mTextVisible) {
166-
textWidth = mPaint.measureText(text);
167-
} else {
168-
textWidth = 0;
169-
}
165+
float textWidth = mTextVisible ? mPaint.measureText(text) : 0;
170166
float progressPosX = mRealWidth * (mProgress * 1.0f / getMax());
171167
float textPosX = progressPosX;
172168
float reachedEndX;
173169
if (mTextAlign == TEXT_ALIGN_MIDDLE) {
174-
reachedEndX = progressPosX - mTextOffset;
170+
if (mProgress >= 95) {
171+
reachedEndX = mRealWidth - textWidth - mTextOffset;
172+
} else {
173+
reachedEndX = progressPosX - mTextOffset;
174+
}
175175
} else {
176176
reachedEndX = progressPosX;
177177
}

0 commit comments

Comments
 (0)