Skip to content

Commit d38f601

Browse files
author
Joao Rutkoski
committed
color span via databinding
1 parent eda15e0 commit d38f601

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

app/src/main/java/com/opencraft/android/gradienttextview/sample/MainViewModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ import com.opencraft.android.gradienttextview.R
66

77
class MainViewModel(context: Context) {
88
val text = ObservableField<String>("aaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa")
9-
val colors = context.resources.getIntArray(R.array.gradient_default)
9+
val colors = context.resources.getIntArray(R.array.gradient_2)
10+
val colorsSpan = context.resources.getIntArray(R.array.gradient_color_span_2)
1011
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
android:text="aaaaaaaaaaaaa\naaaaaaaaaaaaa\naaaaaaaaaaaaa\naaaaaaaaaaaaaa"
2525
android:textSize="22sp"
2626
android:textStyle="bold"
27-
app:gradient_end="BOTTOM_RIGHT"
28-
app:gradient_start="TOP_LEFT"
27+
app:gradientColorsSpan="@{vm.colorsSpan}"
2928
app:gradientColors="@{vm.colors}"
30-
/>
29+
app:gradient_end="BOTTOM_RIGHT"
30+
app:gradient_start="TOP_LEFT" />
3131

3232
<com.opencraft.library.GradientTextView
3333
android:id="@+id/gradient"
@@ -38,8 +38,8 @@
3838
android:textSize="22sp"
3939
android:textStyle="bold"
4040
app:angle="-45"
41-
app:gradientColors="@array/gradient_default"
42-
app:color_span_array="@array/gradient_color_span_default" />
41+
app:color_span_array="@array/gradient_color_span_default"
42+
app:gradientColors="@array/gradient_default" />
4343

4444
</LinearLayout>
4545
</layout>

library/src/main/java/com/opencraft/library/GradientTextView.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class GradientTextView constructor(context: Context, attrs: AttributeSet?, defSt
2424
invalidate()
2525
}
2626

27+
var gradientColorsSpan = intArrayOf(1, 1)
28+
set(value) {
29+
field = value
30+
changedColors = true
31+
invalidate()
32+
}
33+
2734
init {
2835
attrs?.let {
2936

@@ -42,6 +49,7 @@ class GradientTextView constructor(context: Context, attrs: AttributeSet?, defSt
4249
}
4350

4451
private fun getLinearGradient(): LinearGradient {
52+
calcColorPos()
4553
val gradientStartPoint = getStartPoint()
4654
val gradientEndPoint = getEndPoint()
4755
return LinearGradient(gradientStartPoint.x.toFloat(),
@@ -63,9 +71,12 @@ class GradientTextView constructor(context: Context, attrs: AttributeSet?, defSt
6371
}
6472

6573
private fun loadColorsSpan(typedArray: TypedArray) {
66-
val colorsSpan = resources.getIntArray(typedArray
74+
gradientColorsSpan = resources.getIntArray(typedArray
6775
.getResourceId(R.styleable.GradientTextView_color_span_array, R.array.gradient_color_span_default))
68-
colorsSpan?.let {
76+
}
77+
78+
private fun calcColorPos() {
79+
gradientColorsSpan.let {
6980
gradientColorPos = FloatArray(it.size)
7081
gradientColorPos[0] = 0f //first color must be at the start
7182
val sum = it.sum().toFloat()
@@ -77,7 +88,7 @@ class GradientTextView constructor(context: Context, attrs: AttributeSet?, defSt
7788
}
7889

7990
private fun loadColors(typedArray: TypedArray) {
80-
gradientColors = resources.getIntArray(typedArray
91+
gradientColors = resources.getIntArray(typedArray
8192
.getResourceId(R.styleable.GradientTextView_gradientColors, R.array.gradient_default))
8293
}
8394

library/src/main/res/values/arrays.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@
44
<item>@android:color/holo_red_dark</item>
55
<item>@android:color/holo_blue_bright</item>
66
</integer-array>
7-
7+
<integer-array name="gradient_2">
8+
<item>@android:color/holo_red_dark</item>
9+
<item>@android:color/white</item>
10+
<item>@android:color/holo_blue_bright</item>
11+
</integer-array>
12+
813
<integer-array name="gradient_color_span_default">
914
<item>1</item>
1015
<item>1</item>
1116
</integer-array>
17+
18+
<integer-array name="gradient_color_span_2">
19+
<item>1</item>
20+
<item>1</item>
21+
<item>1</item>
22+
</integer-array>
1223
</resources>

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<declare-styleable name="GradientTextView">
44
<attr name="color_array" format="reference" />
55
<attr name="gradientColors" format="reference" />
6+
<attr name="gradientColorsSpan" format="reference" />
67
<attr name="color_span_array" format="reference" />
78
<attr name="angle" format="integer" />
89
<attr name="gradient_start" format="enum">

0 commit comments

Comments
 (0)