Commit a1562c1a by likorn

Progress bar color is animated

parent 50897d67
package com.example.quickmax
import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.os.Build
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private lateinit var answerSet: AnswerSet
private val timeToSolve = 4000
private val timeToSolve:Long = 4000
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -19,6 +22,7 @@ class MainActivity : AppCompatActivity() {
answerSet = AnswerSet(3)
setUpAnswerButtons()
timer.start()
startProgressBarAnimation()
}
private fun setUpAnswerButtons() {
......@@ -30,7 +34,7 @@ class MainActivity : AppCompatActivity() {
private fun processAnswer(correct: Boolean) {
timer.cancel()
makeRadioButtonsUncheckable()
makeButtonsUncheckable()
val responseFragment = ResponseFragment.newInstance().also {
f -> f.arguments = Bundle().also {
......@@ -42,29 +46,41 @@ class MainActivity : AppCompatActivity() {
.commitAllowingStateLoss()
}
private fun makeRadioButtonsUncheckable() {
private fun makeButtonsUncheckable() {
for (answer in answerSet) {
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() {
val intent = intent
finish()
startActivity(intent)
}
private val timer = object : CountDownTimer(timeToSolve.toLong(), 100) {
private val timer = object : CountDownTimer(timeToSolve, 100) {
override fun onTick(millisUntilFinished: Long) {
val progress = ((timeToSolve - millisUntilFinished).toFloat() / timeToSolve) * 100
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
bar_time_left.setProgress(progress.toInt(), true)
progress_bar.setProgress(progress.toInt(), true)
else
bar_time_left.progress = progress.toInt()
progress_bar.progress = progress.toInt()
}
override fun onFinish() {
makeRadioButtonsUncheckable()
makeButtonsUncheckable()
supportFragmentManager
.beginTransaction()
.add(R.id.main_layout, TimeIsOverFragment.newInstance(), "time_is_over")
......
......@@ -25,13 +25,14 @@
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="@+id/bar_time_left"
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_gravity="center"
android:max="100"
android:indeterminate="false" />
android:foregroundTint="#320A64"
android:indeterminate="false"
android:max="100" />
</FrameLayout>
......
......@@ -7,5 +7,8 @@
<color name="colorBackground">#C4BFCC</color>
<color name="colorCorrect">#81C784</color>
<color name="gradient_dark">#FF4A148C</color>
<color name="gradient_very_dark">#FF320A64</color>
<color name="gradient_light">#FF00BCD4</color>
<color name="white">#FFF</color>
<color name="red">#F00</color>
</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