I want to change the background of a button while being pressed by filling it a color from left to right just like a horizontal progress bar.
This is the code for detecting press-hold:
lateinit var buttonView: View
val cdt = object : CountDownTimer(1000, 100) {
override fun onTick(millisUntilFinished: Long) {
}
override fun onFinish() {
//do something after being press by 1 sec.
}
}
val onTouchListener = View.OnTouchListener { v, event ->
buttonView = v
when (event.action) {
MotionEvent.ACTION_DOWN -> {
(v.background as AnimationDrawable).start()
cdt.start()
}
MotionEvent.ACTION_UP -> {
(v.background as AnimationDrawable).stop()
cdt.cancel()
}
}
false
}
This is the background I used for the button:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
...items with drawables which colors are in different position. Frame by frame....
</animation-list>
This works but it ain't smooth. Is there a way to do this efficiently? Just like this? https://codepen.io/kay8/pen/azKbjN
(It's okay to answer in Java)
