1919import android .graphics .Canvas ;
2020import android .graphics .Paint ;
2121import android .graphics .Path ;
22- import android .graphics .Rect ;
2322import android .graphics .RectF ;
2423import android .graphics .Typeface ;
2524import 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