44import android .content .res .ColorStateList ;
55import android .content .res .TypedArray ;
66import android .graphics .Typeface ;
7+ import android .graphics .drawable .GradientDrawable ;
78import android .support .annotation .ColorInt ;
89import android .support .annotation .Nullable ;
910import android .support .annotation .StringRes ;
@@ -82,6 +83,12 @@ public class EasyTextView extends TextView {
8283 private int iconIndex = 0 ;
8384 //是否开启计算文字边界,开启后会以最大文字大小为View高度,并且会增加部分文字高度,防止部分英文类型y,g由于基线的原因无法显示完全
8485 private boolean autoMaxHeight ;
86+ //渐变色
87+ private ColorStateList startColor = null ;
88+ private ColorStateList centerColor = null ;
89+ private ColorStateList endColor = null ;
90+ //渐变方向
91+ GradientDrawable .Orientation orientation ;
8592
8693 public EasyTextView (Context context ) {
8794 this (context , null );
@@ -146,8 +153,8 @@ private void initIconFont() {
146153
147154 if (!TextUtils .isEmpty (mTextRight )) {
148155 AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan ((int ) mTextPadding );
149- stringBuilder .setSpan (sizeSpan , iconIndex + centerSize , iconIndex + centerSize + 1 , Spanned
150- .SPAN_EXCLUSIVE_EXCLUSIVE );
156+ stringBuilder .setSpan (sizeSpan , iconIndex + centerSize , iconIndex +
157+ centerSize + 1 , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
151158 }
152159
153160 }
@@ -207,7 +214,8 @@ private void initIconFont() {
207214 for (SpanContainer container : rightContainer ) {
208215 for (Object o : container .spans ) {
209216 try {
210- stringBuilder .setSpan (o , start + container .start , start + container .end , container .flag );
217+ stringBuilder .setSpan (o , start + container .start , start + container .end ,
218+ container .flag );
211219 } catch (Exception e ) {
212220 //please check invoke clearSpan() method first
213221 }
@@ -280,7 +288,8 @@ private void setLeftTextAttr(SpannableStringBuilder stringBuilder) {
280288 }
281289 }
282290
283- private void initTextSize (SpannableStringBuilder stringBuilder , int start , int end , float textSize , int mCurColor ) {
291+ private void initTextSize (SpannableStringBuilder stringBuilder , int start , int end , float
292+ textSize , int mCurColor ) {
284293 if (textSize != 0 ) {
285294 CharacterStyle sizeSpan ;
286295 final int gravity = getGravity () & Gravity .VERTICAL_GRAVITY_MASK ;
@@ -308,7 +317,8 @@ private void initTextLeftColor(SpannableStringBuilder stringBuilder, int end) {
308317 }
309318 }
310319
311- private void initTextStyle (int textStyle , SpannableStringBuilder stringBuilder , int start , int end ) {
320+ private void initTextStyle (int textStyle , SpannableStringBuilder stringBuilder , int start ,
321+ int end ) {
312322 StyleSpan span ;
313323 if (textStyle != Typeface .NORMAL ) {
314324 span = new StyleSpan (textStyle );
@@ -327,14 +337,53 @@ private void initShape() {
327337 }
328338
329339 private void setShape () {
340+ ShapeBuilder shapeBuilder ;
330341 if (mRadius != 0 ) {
331- ShapeBuilder .create ().Type (type ).Radius (mRadius ).Soild (mSoild ).Stroke
332- (mStrokeWidth , mStrokeColor ).build (this );
342+ shapeBuilder = ShapeBuilder .create ().Type (type ).Radius (mRadius ).Stroke
343+ (mStrokeWidth , mStrokeColor );
344+ } else {
345+ shapeBuilder = ShapeBuilder .create ().Type (type ).Radius (mRadiusTopLeft ,
346+ mRadiusTopRight , mRadiusBottomLeft , mRadiusBottomRight ).Stroke
347+ (mStrokeWidth , mStrokeColor );
348+ }
349+ if (orientation != null && startColor != null && endColor != null ) {
350+ //渐变
351+ if (centerColor != null ) {
352+ shapeBuilder .Gradient (orientation , getColor (startColor ), getColor (centerColor ),
353+ getColor (endColor ));
354+ } else {
355+ shapeBuilder .GradientInit (orientation , getColor (startColor ), getColor (endColor ));
356+ }
333357 } else {
334- ShapeBuilder .create ().Type (type ).Radius (mRadiusTopLeft ,
335- mRadiusTopRight , mRadiusBottomLeft , mRadiusBottomRight ).Soild (mSoild ).Stroke
336- (mStrokeWidth , mStrokeColor ).build (this );
358+ shapeBuilder .Solid (mSoild );
359+ }
360+ shapeBuilder .build (this );
361+ }
362+
363+ private int getColor (ColorStateList color ) {
364+ return color .getColorForState (getDrawableState (), 0 );
365+ }
366+
367+ private GradientDrawable .Orientation switchEnumToOrientation (int orientation ) {
368+ switch (orientation ) {
369+ case 0 :
370+ return GradientDrawable .Orientation .TOP_BOTTOM ;
371+ case 1 :
372+ return GradientDrawable .Orientation .TR_BL ;
373+ case 2 :
374+ return GradientDrawable .Orientation .RIGHT_LEFT ;
375+ case 3 :
376+ return GradientDrawable .Orientation .BR_TL ;
377+ case 4 :
378+ return GradientDrawable .Orientation .BOTTOM_TOP ;
379+ case 5 :
380+ return GradientDrawable .Orientation .BL_TR ;
381+ case 6 :
382+ return GradientDrawable .Orientation .LEFT_RIGHT ;
383+ case 7 :
384+ return GradientDrawable .Orientation .TL_BR ;
337385 }
386+ return GradientDrawable .Orientation .LEFT_RIGHT ;
338387 }
339388
340389 private void clearText () {
@@ -377,8 +426,6 @@ private void initAttr(Context context, AttributeSet attrs) {
377426 mTextRight = textValue .string ;
378427 }
379428 }
380- //mTextLeft = array.getString(R.styleable.EasyTextView_textLeft);
381- //mTextRight = array.getString(R.styleable.EasyTextView_textRight);
382429 mIconColor = array .getColorStateList (R .styleable .EasyTextView_iconColor );
383430 mLeftColor = array .getColorStateList (R .styleable .EasyTextView_textLeftColor );
384431 mRightColor = array .getColorStateList (R .styleable .EasyTextView_textRightColor );
@@ -388,6 +435,11 @@ private void initAttr(Context context, AttributeSet attrs) {
388435 mTextRightStyle = array .getInt (R .styleable .EasyTextView_textRightStyle , Typeface .NORMAL );
389436 mTextCenterStyle = array .getInt (R .styleable .EasyTextView_textCenterStyle , Typeface .NORMAL );
390437 autoMaxHeight = array .getBoolean (R .styleable .EasyTextView_autoMaxHeight , false );
438+ orientation = switchEnumToOrientation (array .getInt (R .styleable
439+ .EasyTextView_gradientOrientation , 0 ));
440+ startColor = array .getColorStateList (R .styleable .EasyTextView_startSolid );
441+ centerColor = array .getColorStateList (R .styleable .EasyTextView_centerSolid );
442+ endColor = array .getColorStateList (R .styleable .EasyTextView_endSolid );
391443 array .recycle ();
392444 }
393445
@@ -859,14 +911,19 @@ public EasyTextView build() {
859911 }
860912
861913 protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
862- super .onMeasure (widthMeasureSpec , heightMeasureSpec );
863- if (this .autoMaxHeight ) {
914+ try {
915+ super .onMeasure (widthMeasureSpec , heightMeasureSpec );
916+ } catch (Exception e ) {
917+
918+ }
919+ if (this .autoMaxHeight ) {
864920 int lead = 0 ;
865- if (this .getPaint () != null ) {
921+ if (this .getPaint () != null ) {
866922 lead = this .getPaint ().getFontMetricsInt ().leading * 3 ;
867923 }
868-
869- this .setMeasuredDimension (this .getMeasuredWidth (), (int )(Math .max ((float )this .getMeasuredHeight (), Math .max (this .mLeftSize , this .mRightSize )) + (float )lead ));
924+ this .setMeasuredDimension (this .getMeasuredWidth (), (int ) (Math .max ((float ) this
925+ .getMeasuredHeight (), Math .max (this .mLeftSize , this .mRightSize )) + (float )
926+ lead ));
870927 }
871928
872929 }
0 commit comments