33import android .graphics .Color ;
44import android .graphics .LinearGradient ;
55import android .graphics .Shader ;
6+ import android .util .Log ;
67import android .widget .TextView ;
78
89public class Tvg {
@@ -14,6 +15,7 @@ public class Tvg {
1415 * @param endColor end color of gradient
1516 */
1617 public static void change (TextView tv , int startColor , int endColor ){
18+ tv .setTextColor (startColor );
1719 Shader textShader = new LinearGradient (0 , 0 , tv .getPaint ().measureText (tv .getText ().toString ()), tv .getTextSize (),
1820 new int []{startColor , endColor },
1921 new float []{0 , 1 }, Shader .TileMode .CLAMP );
@@ -27,9 +29,16 @@ public static void change(TextView tv, int startColor, int endColor){
2729 * @param colors arrays of multiple colors to create the gradient
2830 */
2931 public static void change (TextView tv , int [] colors ){
30- Shader textShader = new LinearGradient (0 , 0 , tv .getPaint ().measureText (tv .getText ().toString ()), tv .getTextSize (),
31- colors ,
32- null , Shader .TileMode .CLAMP );
33- tv .getPaint ().setShader (textShader );
32+ try {
33+ tv .setTextColor (colors [0 ]);
34+ Shader textShader = new LinearGradient (0 , 0 , tv .getPaint ().measureText (tv .getText ().toString ()), tv .getTextSize (),
35+ colors ,
36+ null , Shader .TileMode .CLAMP );
37+ tv .getPaint ().setShader (textShader );
38+ }catch (Exception e ){
39+ Log .e (Tvg .class .getSimpleName (), e .toString ());
40+ }
41+
42+
3443 }
3544}
0 commit comments