99import android .graphics .LinearGradient ;
1010import android .graphics .Paint ;
1111import android .graphics .Shader ;
12- import android .graphics .drawable .Drawable ;
1312import android .text .Layout ;
1413import android .text .SpannableString ;
1514import android .text .TextPaint ;
1615import android .text .style .LeadingMarginSpan ;
1716import android .util .AttributeSet ;
1817import android .util .LayoutDirection ;
19- import android .util .Log ;
2018
2119import androidx .annotation .ColorInt ;
20+ import androidx .annotation .Nullable ;
2221import androidx .core .text .TextUtilsCompat ;
2322
2423import com .flyjingfish .perfecttextviewlib .PerfectTextView ;
2524
2625import java .util .ArrayList ;
26+ import java .util .Arrays ;
2727import java .util .List ;
2828import java .util .Locale ;
2929
@@ -148,7 +148,7 @@ protected void drawableStateChanged() {
148148 updateColors ();
149149 }
150150
151- private void updateColors (){
151+ private boolean updateColors (){
152152 boolean inval = false ;
153153 final int [] drawableState = getDrawableState ();
154154 int color = strokeTextColor .getColorForState (drawableState , 0 );
@@ -214,28 +214,32 @@ private void updateColors(){
214214 if (inval ){
215215 invalidate ();
216216 }
217+ return inval ;
217218 }
219+
220+ @ SuppressLint ("DrawAllocation" )
218221 @ Override
219222 protected void onDraw (Canvas canvas ) {
220223 TextPaint textPaint = getPaint ();
221224 Paint .Style oldStyle = textPaint .getStyle ();
222225 textPaint .setStrokeWidth (strokeWidth );
223226 textPaint .setStyle (Paint .Style .FILL_AND_STROKE );
224227 textPaint .setStrokeJoin (strokeJoin );
228+ LinearGradient linearGradient ;
225229 if (gradientStrokeColor ){
226230 float currentAngle = strokeAngle ;
227231 if (strokeRtlAngle && isRtl ){
228232 currentAngle = - strokeAngle ;
229233 }
230234 float [] xy = getAngleXY (currentAngle );
231235
232- @ SuppressLint ("DrawAllocation" ) LinearGradient linearGradient = new LinearGradient (xy [0 ], xy [1 ], xy [2 ], xy [3 ], gradientStrokeColors , gradientStrokePositions , Shader .TileMode .CLAMP );
233- textPaint .setShader (linearGradient );
236+ linearGradient = new LinearGradient (xy [0 ], xy [1 ], xy [2 ], xy [3 ], gradientStrokeColors , gradientStrokePositions , Shader .TileMode .CLAMP );
234237 }else {
235- @ SuppressLint ("DrawAllocation" ) LinearGradient linearGradient = new LinearGradient (0 , 0 ,getWidth (),getHeight (), new int []{strokeTextColor ,strokeTextColor }, null , Shader .TileMode .CLAMP );
236- textPaint .setShader (linearGradient );
238+ linearGradient = new LinearGradient (0 , 0 ,getWidth (),getHeight (), new int []{curStrokeTextColor ,curStrokeTextColor }, null , Shader .TileMode .CLAMP );
237239 }
240+ textPaint .setShader (linearGradient );
238241 super .onDraw (canvas );
242+
239243 textPaint .setStrokeWidth (0 );
240244 textPaint .setStyle (oldStyle );
241245 if (gradientColor ){
@@ -245,11 +249,11 @@ protected void onDraw(Canvas canvas) {
245249 }
246250 float [] xy = getAngleXY (currentAngle );
247251
248- @ SuppressLint ("DrawAllocation" ) LinearGradient linearGradient = new LinearGradient (xy [0 ], xy [1 ], xy [2 ], xy [3 ], gradientColors , gradientPositions , Shader .TileMode .CLAMP );
249- getPaint ().setShader (linearGradient );
252+ linearGradient = new LinearGradient (xy [0 ], xy [1 ], xy [2 ], xy [3 ], gradientColors , gradientPositions , Shader .TileMode .CLAMP );
250253 }else {
251- getPaint (). setShader ( null ) ;
254+ linearGradient = null ;
252255 }
256+ textPaint .setShader (linearGradient );
253257 super .onDraw (canvas );
254258
255259 }
@@ -318,13 +322,41 @@ public int[] getGradientStrokeColors() {
318322 return gradientStrokeColors ;
319323 }
320324
321- public void setGradientStrokeColors (int [] gradientStrokeColors ) {
322- this .gradientStrokeColors = gradientStrokeColors ;
323- gradientStrokeColor = gradientStrokeColors != null ;
324- if (gradientStrokePositions != null && gradientStrokeColors != null && gradientStrokeColors .length != gradientStrokePositions .length ){
325- this .gradientStrokePositions = null ;
325+ public List <ColorStateList > getGradientStrokeColorStates () {
326+ return gradientStrokeColorStates ;
327+ }
328+
329+ public void setGradientStrokeColors (@ Nullable @ ColorInt int [] gradientStrokeColors ) {
330+ ColorStateList [] colorStateLists ;
331+ if (gradientStrokeColors != null ){
332+ colorStateLists = new ColorStateList [gradientStrokeColors .length ];
333+ for (int i = 0 ; i < gradientStrokeColors .length ; i ++) {
334+ colorStateLists [i ] = ColorStateList .valueOf (gradientStrokeColors [i ]);
335+ }
336+ }else {
337+ colorStateLists = null ;
338+ }
339+ setGradientStrokeColors (colorStateLists );
340+ }
341+
342+ public void setGradientStrokeColors (@ Nullable ColorStateList [] colorStateLists ) {
343+ gradientStrokeColorStates .clear ();
344+ if (colorStateLists != null ){
345+ gradientStrokeColorStates .addAll (Arrays .asList (colorStateLists ));
346+ if (gradientStrokeColorStates .size () == 1 ){
347+ gradientStrokeColorStates .add (ColorStateList .valueOf (Color .TRANSPARENT ));
348+ }
349+ gradientStrokeColor = gradientStrokeColorStates .size () > 0 ;
350+ if (gradientStrokePositions != null && gradientStrokeColorStates .size () != gradientStrokePositions .length ){
351+ this .gradientStrokePositions = null ;
352+ }
353+ updateColors ();
354+ }else {
355+ gradientStrokeColor = false ;
356+ if (!updateColors ()){
357+ invalidate ();
358+ }
326359 }
327- invalidate ();
328360 }
329361
330362 public float [] getGradientStrokePositions () {
@@ -341,12 +373,40 @@ public int[] getGradientColors() {
341373 return gradientColors ;
342374 }
343375
344- public void setGradientColors (int [] gradientColors ) {
345- this .gradientColors = gradientColors ;
346- if (gradientPositions != null && gradientColors != null && gradientColors .length != gradientPositions .length ){
347- this .gradientPositions = null ;
376+ public List <ColorStateList > getGradientColorStates () {
377+ return gradientColorStates ;
378+ }
379+ public void setGradientColors (@ Nullable @ ColorInt int [] gradientColors ) {
380+ ColorStateList [] colorStateLists ;
381+ if (gradientColors != null ){
382+ colorStateLists = new ColorStateList [gradientColors .length ];
383+ for (int i = 0 ; i < gradientColors .length ; i ++) {
384+ colorStateLists [i ] = ColorStateList .valueOf (gradientColors [i ]);
385+ }
386+ }else {
387+ colorStateLists = null ;
388+ }
389+ setGradientColors (colorStateLists );
390+ }
391+
392+ public void setGradientColors (@ Nullable ColorStateList [] colorStateLists ) {
393+ gradientColorStates .clear ();
394+ if (colorStateLists != null ){
395+ gradientColorStates .addAll (Arrays .asList (colorStateLists ));
396+ if (gradientColorStates .size () == 1 ){
397+ gradientColorStates .add (ColorStateList .valueOf (Color .TRANSPARENT ));
398+ }
399+ gradientStrokeColor = gradientColorStates .size () > 0 ;
400+ if (gradientPositions != null && gradientColorStates .size () != gradientPositions .length ){
401+ this .gradientPositions = null ;
402+ }
403+ updateColors ();
404+ }else {
405+ gradientStrokeColor = false ;
406+ if (!updateColors ()){
407+ invalidate ();
408+ }
348409 }
349- invalidate ();
350410 }
351411
352412 public float [] getGradientPositions () {
@@ -415,6 +475,18 @@ public void setStrokeTextColors(ColorStateList strokeTextColor) {
415475 updateColors ();
416476 }
417477
478+ @ Override
479+ public void setTextColor (int color ) {
480+ gradientColor = false ;
481+ super .setTextColor (color );
482+ }
483+
484+ @ Override
485+ public void setTextColor (ColorStateList colors ) {
486+ gradientColor = false ;
487+ super .setTextColor (colors );
488+ }
489+
418490 @ Override
419491 public void setText (CharSequence text , BufferType type ) {
420492 if (strokeWidth > 0 ){
@@ -428,5 +500,6 @@ public void setText(CharSequence text, BufferType type) {
428500 */
429501 public void setStrokeJoin (Paint .Join join ){
430502 strokeJoin = join ;
503+ invalidate ();
431504 }
432505}
0 commit comments