Skip to content

Commit

Permalink
Optimize progress
Browse files Browse the repository at this point in the history
  • Loading branch information
liyujiang-gzu committed May 28, 2021
1 parent 095b0dd commit c8c43f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:progress="50"
android:progress="99"
android:visibility="visible"
app:lyj_cut_corner="ellipse"
app:lyj_reached_color="#00FBD0"
Expand All @@ -102,7 +102,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:progress="50"
android:progress="99"
android:visibility="visible"
app:lyj_cut_corner="ellipse"
app:lyj_reached_color="#00FBD0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
Expand Down Expand Up @@ -80,6 +79,7 @@ private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EnhancedProgressBar);
mProgress = a.getInt(R.styleable.EnhancedProgressBar_android_progress, 0);
mProgress = Math.min(mProgress, getMax());
mTextColor = a.getColor(R.styleable.EnhancedProgressBar_lyj_text_color, DEFAULT_TEXT_COLOR);
mTextSize = (int) a.getDimension(R.styleable.EnhancedProgressBar_lyj_text_size, mTextSize);
mReachedBarColor = a.getColor(R.styleable.EnhancedProgressBar_lyj_reached_color, mTextColor);
Expand All @@ -99,6 +99,7 @@ private void init(Context context, AttributeSet attrs) {
}
mBarHeight = Math.max(mReachedBarHeight, mUnReachedBarHeight);
mPaint.setAntiAlias(true);
mPaint.setTypeface(Typeface.MONOSPACE);
mPaint.setTextSize(mTextSize);
mPaint.setColor(mTextColor);
}
Expand Down Expand Up @@ -145,13 +146,13 @@ public synchronized int getProgress() {

@Override
public synchronized void setProgress(int progress) {
mProgress = progress;
mProgress = Math.min(progress, getMax());
invalidate();
}

@Override
public void setProgress(int progress, boolean animate) {
mProgress = progress;
mProgress = Math.min(progress, getMax());
invalidate();
}

Expand All @@ -161,17 +162,16 @@ protected synchronized void onDraw(Canvas canvas) {
canvas.translate(getPaddingLeft(), getHeight() / 2f);
boolean needDrawUnreachedBar = true;
String text = mProgress + "%";
float textWidth;
if (mTextVisible) {
textWidth = mPaint.measureText(text);
} else {
textWidth = 0;
}
float textWidth = mTextVisible ? mPaint.measureText(text) : 0;
float progressPosX = mRealWidth * (mProgress * 1.0f / getMax());
float textPosX = progressPosX;
float reachedEndX;
if (mTextAlign == TEXT_ALIGN_MIDDLE) {
reachedEndX = progressPosX - mTextOffset;
if (mProgress >= 95) {
reachedEndX = mRealWidth - textWidth - mTextOffset;
} else {
reachedEndX = progressPosX - mTextOffset;
}
} else {
reachedEndX = progressPosX;
}
Expand Down

0 comments on commit c8c43f4

Please sign in to comment.