99import android .graphics .Shader ;
1010import android .graphics .drawable .Drawable ;
1111import android .text .Layout ;
12+ import android .text .SpannableString ;
1213import android .text .TextPaint ;
14+ import android .text .style .LeadingMarginSpan ;
1315import android .util .AttributeSet ;
1416import android .util .LayoutDirection ;
1517import android .view .ViewGroup ;
@@ -107,9 +109,15 @@ public GradientTextView(Context context, AttributeSet attrs,
107109
108110 initCompoundDrawables ();
109111
110-
112+ CharSequence text = getText ();
113+ setText (text );
111114 }
112115
116+ static SpannableString createIndentedText (CharSequence text , int marginFirstLine , int marginNextLines ) {
117+ SpannableString result = new SpannableString (text );
118+ result .setSpan (new LeadingMarginSpan .Standard (marginFirstLine , marginNextLines ), 0 , text .length (), 0 );
119+ return result ;
120+ }
113121
114122 @ Override
115123 public void setLayoutParams (ViewGroup .LayoutParams params ) {
@@ -119,12 +127,25 @@ public void setLayoutParams(ViewGroup.LayoutParams params) {
119127
120128 @ Override
121129 protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
130+ int widthMode = MeasureSpec .getMode (widthMeasureSpec );
122131 CharSequence tt = backGroundText .getText ();
123132 if (tt == null || !tt .equals (this .getText ())) {
124133 backGroundText .setText (getText ());
125134 }
126135 backGroundText .measure (widthMeasureSpec , heightMeasureSpec );
127136 super .onMeasure (widthMeasureSpec , heightMeasureSpec );
137+ if (widthMode == MeasureSpec .AT_MOST && strokeWidth > 0 ){
138+ int measureWidth = getMeasuredWidth ();
139+ int width = MeasureSpec .getSize (widthMeasureSpec );
140+ if (measureWidth < width ){
141+ int measureHeight = getMeasuredHeight ();
142+ // int height = MeasureSpec.getSize(heightMeasureSpec);
143+ int heightMode = MeasureSpec .getMode (heightMeasureSpec );
144+ backGroundText .measure (widthMeasureSpec , heightMeasureSpec );
145+ int newWidth = MeasureSpec .makeMeasureSpec (measureWidth +Math .min (strokeWidth /2 ,width -measureWidth ), widthMode );
146+ setMeasuredDimension (newWidth ,MeasureSpec .makeMeasureSpec (measureHeight , heightMode ));
147+ }
148+ }
128149 }
129150
130151 @ Override
@@ -323,10 +344,11 @@ public void setStrokeTextColor(int strokeTextColor) {
323344
324345 @ Override
325346 public void setText (CharSequence text , BufferType type ) {
347+ SpannableString spannableString = createIndentedText (text , strokeWidth /2 , strokeWidth /2 );
326348 if (backGroundText != null ){
327- backGroundText .setText (text , type );
349+ backGroundText .setText (spannableString , type );
328350 }
329- super .setText (text , type );
351+ super .setText (spannableString , type );
330352 }
331353
332354 @ Override
0 commit comments