Commit a1562c1a by likorn

Progress bar color is animated

parent 50897d67
package com.example.quickmax package com.example.quickmax
import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.CountDownTimer import android.os.CountDownTimer
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private lateinit var answerSet: AnswerSet private lateinit var answerSet: AnswerSet
private val timeToSolve = 4000 private val timeToSolve:Long = 4000
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -19,6 +22,7 @@ class MainActivity : AppCompatActivity() { ...@@ -19,6 +22,7 @@ class MainActivity : AppCompatActivity() {
answerSet = AnswerSet(3) answerSet = AnswerSet(3)
setUpAnswerButtons() setUpAnswerButtons()
timer.start() timer.start()
startProgressBarAnimation()
} }
private fun setUpAnswerButtons() { private fun setUpAnswerButtons() {
...@@ -30,7 +34,7 @@ class MainActivity : AppCompatActivity() { ...@@ -30,7 +34,7 @@ class MainActivity : AppCompatActivity() {
private fun processAnswer(correct: Boolean) { private fun processAnswer(correct: Boolean) {
timer.cancel() timer.cancel()
makeRadioButtonsUncheckable() makeButtonsUncheckable()
val responseFragment = ResponseFragment.newInstance().also { val responseFragment = ResponseFragment.newInstance().also {
f -> f.arguments = Bundle().also { f -> f.arguments = Bundle().also {
...@@ -42,29 +46,41 @@ class MainActivity : AppCompatActivity() { ...@@ -42,29 +46,41 @@ class MainActivity : AppCompatActivity() {
.commitAllowingStateLoss() .commitAllowingStateLoss()
} }
private fun makeRadioButtonsUncheckable() { private fun makeButtonsUncheckable() {
for (answer in answerSet) { for (answer in answerSet) {
findViewById<CardView>(answer.buttonId).isClickable = false findViewById<CardView>(answer.buttonId).isClickable = false
} }
} }
private fun startProgressBarAnimation() {
val colorFrom = ContextCompat.getColor(this, R.color.gradient_dark)
val colorTo = ContextCompat.getColor(this, R.color.red)
val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo)
colorAnimation.duration = timeToSolve
colorAnimation.addUpdateListener { animator ->
progress_bar.progressDrawable.setColorFilter(animator.animatedValue as Int,
android.graphics.PorterDuff.Mode.SRC_ATOP)
}
colorAnimation.start()
}
fun reload() { fun reload() {
val intent = intent val intent = intent
finish() finish()
startActivity(intent) startActivity(intent)
} }
private val timer = object : CountDownTimer(timeToSolve.toLong(), 100) { private val timer = object : CountDownTimer(timeToSolve, 100) {
override fun onTick(millisUntilFinished: Long) { override fun onTick(millisUntilFinished: Long) {
val progress = ((timeToSolve - millisUntilFinished).toFloat() / timeToSolve) * 100 val progress = ((timeToSolve - millisUntilFinished).toFloat() / timeToSolve) * 100
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
bar_time_left.setProgress(progress.toInt(), true) progress_bar.setProgress(progress.toInt(), true)
else else
bar_time_left.progress = progress.toInt() progress_bar.progress = progress.toInt()
} }
override fun onFinish() { override fun onFinish() {
makeRadioButtonsUncheckable() makeButtonsUncheckable()
supportFragmentManager supportFragmentManager
.beginTransaction() .beginTransaction()
.add(R.id.main_layout, TimeIsOverFragment.newInstance(), "time_is_over") .add(R.id.main_layout, TimeIsOverFragment.newInstance(), "time_is_over")
......
...@@ -25,13 +25,14 @@ ...@@ -25,13 +25,14 @@
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<ProgressBar <ProgressBar
android:id="@+id/bar_time_left" android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal" style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="32dp" android:layout_height="32dp"
android:layout_gravity="center" android:layout_gravity="center"
android:max="100" android:foregroundTint="#320A64"
android:indeterminate="false" /> android:indeterminate="false"
android:max="100" />
</FrameLayout> </FrameLayout>
......
...@@ -7,5 +7,8 @@ ...@@ -7,5 +7,8 @@
<color name="colorBackground">#C4BFCC</color> <color name="colorBackground">#C4BFCC</color>
<color name="colorCorrect">#81C784</color> <color name="colorCorrect">#81C784</color>
<color name="gradient_dark">#FF4A148C</color> <color name="gradient_dark">#FF4A148C</color>
<color name="gradient_very_dark">#FF320A64</color>
<color name="gradient_light">#FF00BCD4</color> <color name="gradient_light">#FF00BCD4</color>
<color name="white">#FFF</color>
<color name="red">#F00</color>
</resources> </resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment