Skip to content

Commit c6acc4d

Browse files
committed
array error handle
1 parent ab43e5a commit c6acc4d

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

app/src/main/java/com/captaindroid/gradienttextview/MainActivity.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ protected void onCreate(Bundle savedInstanceState) {
1515
super.onCreate(savedInstanceState);
1616
setContentView(R.layout.activity_main);
1717
TextView tv = findViewById(R.id.tv);
18-
Tvg.change(tv, new int[]{
19-
Color.parseColor("#F97C3C"),
20-
Color.parseColor("#FDB54E"),
21-
Color.parseColor("#64B678"),
22-
Color.parseColor("#478AEA"),
23-
Color.parseColor("#8446CC"),
24-
});
18+
Tvg.change(tv, Color.parseColor("#800CDD"), Color.parseColor("#3BA3F2"));
2519
Tvg.change((TextView) findViewById(R.id.tv2), new int[]{
2620
Color.parseColor("#F97C3C"),
2721
Color.parseColor("#FDB54E"),
@@ -30,11 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
3024
Color.parseColor("#8446CC"),
3125
});
3226
Tvg.change((TextView) findViewById(R.id.tv3), new int[]{
33-
Color.parseColor("#F97C3C"),
34-
Color.parseColor("#FDB54E"),
35-
Color.parseColor("#64B678"),
36-
Color.parseColor("#478AEA"),
37-
Color.parseColor("#8446CC"),
27+
3828
});
3929
}
4030
}

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
android:textStyle="bold"
3535
android:text="This is an another very big big text with long details to test color"
3636
android:textColor="#000"
37-
android:textSize="30sp"
3837
android:gravity="center"
3938
android:id="@+id/tv3"
4039
android:layout_gravity="center"

tvg/src/main/java/com/captaindroid/tvg/Tvg.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.graphics.Color;
44
import android.graphics.LinearGradient;
55
import android.graphics.Shader;
6+
import android.util.Log;
67
import android.widget.TextView;
78

89
public 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

Comments
 (0)