@@ -51,6 +51,8 @@ public class EasyTextView extends TextView {
5151 private int mCurRightColor ;
5252 private float mLeftSize ;
5353 private float mRightSize ;
54+
55+ private SpannableStringBuilder mStringBuilder ;
5456 //icon的index
5557 private int iconIndex = 0 ;
5658
@@ -65,6 +67,8 @@ public EasyTextView(Context context, @Nullable AttributeSet attrs) {
6567 public EasyTextView (Context context , @ Nullable AttributeSet attrs , int defStyleAttr ) {
6668 super (context , attrs , defStyleAttr );
6769 this .mContext = context ;
70+ mStringBuilder = new SpannableStringBuilder ();
71+
6872 initAttr (context , attrs );
6973 init ();
7074 }
@@ -82,99 +86,120 @@ private void initIconFont() {
8286 }
8387 iconString = getText ().toString ();
8488 if (!TextUtils .isEmpty (mTextLeft ) || !TextUtils .isEmpty (mTextRight )) {
85- SpannableStringBuilder stringBuilder = new SpannableStringBuilder (getText ());
89+ mStringBuilder . append (getText ());
8690 //增加空格
8791 if (!TextUtils .isEmpty (mTextLeft )) {
8892 if (mTextPadding != 0 ) {
89- stringBuilder .insert (0 , EMPTY_SPACE );
93+ mStringBuilder .insert (0 , EMPTY_SPACE );
9094 iconIndex ++;
9195 }
92- stringBuilder .insert (0 , mTextLeft );
96+ mStringBuilder .insert (0 , mTextLeft );
9397 iconIndex += mTextLeft .length ();
9498 }
9599
96100 if (!TextUtils .isEmpty (mTextRight )) {
97101 if (mTextPadding != 0 ) {
98- stringBuilder .append (EMPTY_SPACE );
102+ mStringBuilder .append (EMPTY_SPACE );
99103 }
100- stringBuilder .append (mTextRight );
104+ mStringBuilder .append (mTextRight );
101105 }
102- if (mTextPadding != 0 ) {
103- //设置字和icon间距
104- if (!TextUtils .isEmpty (mTextLeft )) {
105- AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan ((int ) mTextPadding );
106- stringBuilder .setSpan (sizeSpan , iconIndex - 1 , iconIndex , Spanned
107- .SPAN_EXCLUSIVE_EXCLUSIVE );
108- }
106+ initTextPadding (mStringBuilder );
107+ //设置icon和字的颜色
108+ initIconColor (mStringBuilder );
109+ initLeftTextColor (mStringBuilder );
110+ initRightColor (mStringBuilder );
109111
110- if (!TextUtils .isEmpty (mTextRight )) {
111- AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan ((int ) mTextPadding );
112- stringBuilder .setSpan (sizeSpan , iconIndex + 1 , iconIndex + 2 , Spanned
113- .SPAN_EXCLUSIVE_EXCLUSIVE );
114- }
112+ //设置字的大小
113+ initLeftTextSize (mStringBuilder );
114+ initRightTextSize (mStringBuilder );
115115
116- }
117- //设置icon和字的颜色
118- if (mIconColor != null ) {
119- int color = mIconColor .getColorForState (getDrawableState (), 0 );
120- if (color != mCurIconColor ) {
121- mCurIconColor = color ;
122- }
123- ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan (mCurIconColor );
124- stringBuilder .setSpan (foregroundColorSpan , iconIndex , iconIndex + 1 , Spanned
116+ setText (mStringBuilder );
117+ }
118+ }
119+
120+ private void initTextPadding (SpannableStringBuilder mStringBuilder ) {
121+ if (mTextPadding != 0 ) {
122+ //设置字和icon间距
123+ if (!TextUtils .isEmpty (mTextLeft )) {
124+ AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan ((int ) mTextPadding );
125+ mStringBuilder .setSpan (sizeSpan , iconIndex - 1 , iconIndex , Spanned
125126 .SPAN_EXCLUSIVE_EXCLUSIVE );
126127 }
127128
128- if (!TextUtils .isEmpty (mTextLeft ) && mLeftColor != null ) {
129- int color = mLeftColor .getColorForState (getDrawableState (), 0 );
130- if (color != mCurLeftColor ) {
131- mCurLeftColor = color ;
132- }
133- int end = mTextPadding == 0 ? iconIndex : iconIndex - 1 ;
134- ForegroundColorSpan foregroundLeftColor = new ForegroundColorSpan (mCurLeftColor );
135- stringBuilder .setSpan (foregroundLeftColor , 0 , end , Spanned
129+ if (!TextUtils .isEmpty (mTextRight )) {
130+ AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan ((int ) mTextPadding );
131+ mStringBuilder .setSpan (sizeSpan , iconIndex + 1 , iconIndex + 2 , Spanned
136132 .SPAN_EXCLUSIVE_EXCLUSIVE );
137133 }
138134
139- if (!TextUtils .isEmpty (mTextRight ) && mRightColor != null ) {
140- int color = mRightColor .getColorForState (getDrawableState (), 0 );
141- if (color != mCurRightColor ) {
142- mCurRightColor = color ;
143- }
144- int start = mTextPadding == 0 ? iconIndex + 1 : iconIndex + 2 ;
145- ForegroundColorSpan foregroundRightColor = new ForegroundColorSpan (mCurRightColor );
146- stringBuilder .setSpan (foregroundRightColor , start , stringBuilder .length ()
147- , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
135+ }
136+ }
137+
138+ private void initRightTextSize (SpannableStringBuilder mStringBuilder ) {
139+ if (!TextUtils .isEmpty (mTextRight ) && mRightSize != 0 ) {
140+ int start = mTextPadding == 0 ? iconIndex + 1 : iconIndex + 2 ;
141+ CharacterStyle sizeSpan ;
142+ final int gravity = getGravity () & Gravity .VERTICAL_GRAVITY_MASK ;
143+ if (gravity == Gravity .CENTER_VERTICAL ) {
144+ sizeSpan = new EasyVerticalCenterSpan (mRightSize ,mCurRightColor );
145+ }else {
146+ sizeSpan = new AbsoluteSizeSpan ((int ) mRightSize );
147+ }
148+ mStringBuilder .setSpan (sizeSpan , start , mStringBuilder .length (), Spanned
149+ .SPAN_EXCLUSIVE_EXCLUSIVE );
150+ }
151+ }
152+
153+ private void initLeftTextSize (SpannableStringBuilder mStringBuilder ) {
154+ if (!TextUtils .isEmpty (mTextLeft ) && mLeftSize != 0 ) {
155+ int end = mTextPadding == 0 ? iconIndex : iconIndex - 1 ;
156+ CharacterStyle sizeSpan ;
157+ final int gravity = getGravity () & Gravity .VERTICAL_GRAVITY_MASK ;
158+ if (gravity == Gravity .CENTER_VERTICAL ) {
159+ sizeSpan = new EasyVerticalCenterSpan (mLeftSize ,mCurLeftColor );
160+ }else {
161+ sizeSpan = new AbsoluteSizeSpan ((int ) mLeftSize );
148162 }
163+ mStringBuilder .setSpan (sizeSpan , 0 , end , Spanned
164+ .SPAN_EXCLUSIVE_EXCLUSIVE );
165+ }
166+ }
149167
150- //设置字的大小
151- if (!TextUtils .isEmpty (mTextLeft ) && mLeftSize != 0 ) {
152- int end = mTextPadding == 0 ? iconIndex : iconIndex - 1 ;
153- CharacterStyle sizeSpan ;
154- final int gravity = getGravity () & Gravity .VERTICAL_GRAVITY_MASK ;
155- if (gravity == Gravity .CENTER_VERTICAL ) {
156- sizeSpan = new EasyVerticalCenterSpan (mLeftSize ,mCurLeftColor );
157- }else {
158- sizeSpan = new AbsoluteSizeSpan ((int ) mLeftSize );
159- }
160- stringBuilder .setSpan (sizeSpan , 0 , end , Spanned
161- .SPAN_EXCLUSIVE_EXCLUSIVE );
168+ private void initRightColor (SpannableStringBuilder mStringBuilder ) {
169+ if (!TextUtils .isEmpty (mTextRight ) && mRightColor != null ) {
170+ int color = mRightColor .getColorForState (getDrawableState (), 0 );
171+ if (color != mCurRightColor ) {
172+ mCurRightColor = color ;
162173 }
174+ int start = mTextPadding == 0 ? iconIndex + 1 : iconIndex + 2 ;
175+ ForegroundColorSpan foregroundRightColor = new ForegroundColorSpan (mCurRightColor );
176+ mStringBuilder .setSpan (foregroundRightColor , start , mStringBuilder .length ()
177+ , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
178+ }
179+ }
163180
164- if (!TextUtils .isEmpty (mTextRight ) && mRightSize != 0 ) {
165- int start = mTextPadding == 0 ? iconIndex + 1 : iconIndex + 2 ;
166- CharacterStyle sizeSpan ;
167- final int gravity = getGravity () & Gravity .VERTICAL_GRAVITY_MASK ;
168- if (gravity == Gravity .CENTER_VERTICAL ) {
169- sizeSpan = new EasyVerticalCenterSpan (mRightSize ,mCurRightColor );
170- }else {
171- sizeSpan = new AbsoluteSizeSpan ((int ) mRightSize );
172- }
173- stringBuilder .setSpan (sizeSpan , start , stringBuilder .length (), Spanned
174- .SPAN_EXCLUSIVE_EXCLUSIVE );
181+ private void initLeftTextColor (SpannableStringBuilder mStringBuilder ) {
182+ if (!TextUtils .isEmpty (mTextLeft ) && mLeftColor != null ) {
183+ int color = mLeftColor .getColorForState (getDrawableState (), 0 );
184+ if (color != mCurLeftColor ) {
185+ mCurLeftColor = color ;
175186 }
187+ int end = mTextPadding == 0 ? iconIndex : iconIndex - 1 ;
188+ ForegroundColorSpan foregroundLeftColor = new ForegroundColorSpan (mCurLeftColor );
189+ mStringBuilder .setSpan (foregroundLeftColor , 0 , end , Spanned
190+ .SPAN_EXCLUSIVE_EXCLUSIVE );
191+ }
192+ }
176193
177- setText (stringBuilder );
194+ private void initIconColor (SpannableStringBuilder mStringBuilder ) {
195+ if (mIconColor != null ) {
196+ int color = mIconColor .getColorForState (getDrawableState (), 0 );
197+ if (color != mCurIconColor ) {
198+ mCurIconColor = color ;
199+ }
200+ ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan (mCurIconColor );
201+ mStringBuilder .setSpan (foregroundColorSpan , iconIndex , iconIndex + 1 , Spanned
202+ .SPAN_EXCLUSIVE_EXCLUSIVE );
178203 }
179204 }
180205
@@ -200,6 +225,8 @@ private void setShape() {
200225 }
201226
202227 private void clearText () {
228+ mStringBuilder .clear ();
229+ mStringBuilder .clearSpans ();
203230 setText (iconString );
204231 iconIndex = 0 ;
205232 }
@@ -232,8 +259,12 @@ protected void drawableStateChanged() {
232259 if (mIconColor != null && mIconColor .isStateful ()
233260 || mLeftColor != null && mLeftColor .isStateful ()
234261 || mRightColor != null && mRightColor .isStateful ()) {
235- clearText ();
236- initIconFont ();
262+ /*clearText();
263+ initIconFont();*/
264+ initIconColor (mStringBuilder );
265+ initLeftTextColor (mStringBuilder );
266+ initRightColor (mStringBuilder );
267+ setText (mStringBuilder );
237268 }
238269 super .drawableStateChanged ();
239270 }
@@ -251,7 +282,7 @@ public void setBackgroundSold(int soild) {
251282 */
252283 public void setIconColor (int color ) {
253284 this .mIconColor = ColorStateList .valueOf (color );
254- build ( );
285+ initIconColor ( mStringBuilder );
255286 }
256287
257288 /**
@@ -275,31 +306,35 @@ public void setTextRight(String textRight) {
275306 */
276307 public void setTextLeftColor (int color ) {
277308 this .mLeftColor = ColorStateList .valueOf (color );
278- build ();
309+ initLeftTextColor (mStringBuilder );
310+ setText (mStringBuilder );
279311 }
280312
281313 /**
282314 * 设置右文案颜色
283315 */
284316 public void setTextRightColor (int color ) {
285317 this .mRightColor = ColorStateList .valueOf (color );
286- build ();
318+ initRightColor (mStringBuilder );
319+ setText (mStringBuilder );
287320 }
288321
289322 /**
290323 * 设置左文案字号大小
291324 */
292325 public void setTextLeftSize (float leftSize ) {
293326 this .mLeftSize = leftSize ;
294- build ();
327+ initLeftTextSize (mStringBuilder );
328+ setText (mStringBuilder );
295329 }
296330
297331 /**
298332 * 设置右文案字号大小
299333 */
300334 public void setTextRightSize (float rightSize ) {
301335 this .mRightSize = rightSize ;
302- build ();
336+ initRightTextSize (mStringBuilder );
337+ setText (mStringBuilder );
303338 }
304339
305340 /**
0 commit comments