Skip to content

Commit

Permalink
adding behavior to recalc gradiente when size changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Renan Rhoden committed Oct 26, 2018
1 parent d38f601 commit 870623d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class MainActivity : AppCompatActivity() {
val vm = MainViewModel(this)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
binding.vm = vm
// binding.gradient.postDelayed({
// binding.gradient.gradientColors = intArrayOf(Color.BLUE, Color.GREEN)
// }, 1000)
binding.gradient.postDelayed({
vm.text.set("aaaaaa\naaaaaa\naaaaaaa\naaaaaaaa")
}, 1000)


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.databinding.ObservableField
import com.opencraft.android.gradienttextview.R

class MainViewModel(context: Context) {
val text = ObservableField<String>("aaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa")
val text = ObservableField<String>()
val colors = context.resources.getIntArray(R.array.gradient_2)
val colorsSpan = context.resources.getIntArray(R.array.gradient_color_span_2)
}
12 changes: 11 additions & 1 deletion library/src/main/java/com/opencraft/library/GradientTextView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class GradientTextView constructor(context: Context, attrs: AttributeSet?, defSt
private var startOption = -1
private var endOption = -1
private var changedColors = true
private var changedSize = false

var gradientColors = intArrayOf(Color.BLACK, Color.WHITE)
set(value) {
Expand Down Expand Up @@ -93,10 +94,14 @@ class GradientTextView constructor(context: Context, attrs: AttributeSet?, defSt
}

override fun onDraw(canvas: Canvas?) {
if (changedColors) {
if (changedColors ) {
changedColors = false
paint.shader = getLinearGradient()
}
if (changedSize) {
changedSize = false
paint.shader = getLinearGradient()
}
super.onDraw(canvas)
}

Expand Down Expand Up @@ -151,6 +156,11 @@ class GradientTextView constructor(context: Context, attrs: AttributeSet?, defSt
return Point(startX.toInt(), startY.toInt())
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
changedColors = true
}

companion object {
const val TOP_LEFT = 0
const val TOP_CENTER = 1
Expand Down

0 comments on commit 870623d

Please sign in to comment.