From c8c43f464b7e84386fdddef2f834b03ca30de5f3 Mon Sep 17 00:00:00 2001 From: liyujiang <1032694760@qq.com> Date: Fri, 28 May 2021 10:28:00 +0800 Subject: [PATCH] Optimize progress --- app/src/main/res/layout/activity_main.xml | 4 ++-- .../progressbar/EnhancedProgressBar.java | 20 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 943c618..cf86cf8 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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" @@ -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" diff --git a/library/src/main/java/com/github/gzuliyujiang/progressbar/EnhancedProgressBar.java b/library/src/main/java/com/github/gzuliyujiang/progressbar/EnhancedProgressBar.java index 32d1c1c..fbad416 100644 --- a/library/src/main/java/com/github/gzuliyujiang/progressbar/EnhancedProgressBar.java +++ b/library/src/main/java/com/github/gzuliyujiang/progressbar/EnhancedProgressBar.java @@ -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; @@ -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); @@ -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); } @@ -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(); } @@ -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; }