Skip to content

Commit f91baf6

Browse files
authored
Merge pull request #10 from JDevZone/dev
optional progress animation
2 parents e1ba357 + 9d8b890 commit f91baf6

File tree

4 files changed

+134
-55
lines changed

4 files changed

+134
-55
lines changed

app/src/main/java/com/devzone/fpl_sample/MainActivity.kt

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.devzone.fpl_sample
33
import android.os.Bundle
44
import android.view.View
55
import android.widget.Button
6+
import android.widget.SeekBar
67
import androidx.appcompat.app.AppCompatActivity
78
import kotlinx.android.synthetic.main.activity_main.*
89

@@ -13,20 +14,54 @@ class MainActivity : AppCompatActivity() {
1314
override fun onCreate(savedInstanceState: Bundle?) {
1415
super.onCreate(savedInstanceState)
1516
setContentView(R.layout.activity_main)
17+
handlePreciseControls()
1618

1719
}
1820

21+
private fun handlePreciseControls() {
22+
progressSeek?.apply {
23+
max = 100
24+
setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
25+
override fun onProgressChanged(
26+
seekBar: SeekBar?,
27+
progress: Int,
28+
fromUser: Boolean
29+
) {
30+
updateLargeProgress(progress)
31+
}
32+
33+
override fun onStartTrackingTouch(seekBar: SeekBar?) {
34+
35+
}
36+
37+
override fun onStopTrackingTouch(seekBar: SeekBar?) {
38+
39+
}
40+
41+
})
42+
}
43+
44+
}
45+
46+
private fun updateLargeProgress(progress: Int) {
47+
val isAnimated = animateCB.isChecked
48+
val isReverse = reverseCB.isChecked
49+
50+
if (isReverse) {
51+
val reverseProgress = kotlin.math.abs(100 - progress)
52+
fillB.setProgress(reverseProgress, isAnimated)
53+
} else
54+
fillB.setProgress(progress, isAnimated)
55+
}
56+
1957
fun toggleFill(view: View) {
2058
val button: Button = view as Button
2159
button.isEnabled = false
22-
2360
isFilled = !isFilled
24-
fillL.setProgress(if (isFilled) 100 else 0)
25-
fillB.setProgress(if (isFilled) 100 else 0)
26-
fillL.setProgressColors(intArrayOf(R.color.colorGradient1,R.color.colorGradient2))
27-
fillB.setDoOnProgressEnd { v ->
28-
button.isEnabled = true;button.text = if (isFilled) "Unfill" else "Fill"
61+
fillL?.setDoOnProgressEnd { v ->
62+
button.isEnabled = true;button.text = if (isFilled) "UnFill" else "Fill"
2963
}
30-
64+
fillL?.setProgress(if (isFilled) 100 else 0, false)
65+
fillL?.setProgressColors(intArrayOf(R.color.colorGradient1, R.color.colorGradient2))
3166
}
3267
}
Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,109 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
4-
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
5-
android:orientation="vertical"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
65
android:layout_width="match_parent"
76
android:layout_height="match_parent"
7+
android:orientation="vertical"
88
tools:context=".MainActivity">
99

1010
<com.devzone.fillprogresslayout.FillProgressLayout
1111
android:id="@+id/fillL"
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content"
1214
android:layout_margin="30dp"
1315
app:fpl_backgroundColor="@color/colorRedTrans"
14-
app:fpl_progressColor="@color/colorGreenTrans"
16+
app:fpl_gradientMovement="true"
1517
app:fpl_isRounded="true"
16-
app:fpl_roundedCornerRadius="100"
1718
app:fpl_progress="0"
18-
app:fpl_gradientMovement="true"
19-
app:fpl_progressDuration="2000"
19+
app:fpl_progressColor="@color/colorGreenTrans"
2020
app:fpl_progressDirection="left_to_right"
21-
app:fpl_shouldRestart="false"
22-
android:layout_width="match_parent"
23-
android:layout_height="wrap_content">
21+
app:fpl_progressDuration="2000"
22+
app:fpl_roundedCornerRadius="100"
23+
app:fpl_shouldRestart="false">
24+
2425
<TextView
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:layout_gravity="center_vertical"
2529
android:layout_weight="1"
26-
android:padding="10dp"
2730
android:gravity="center"
28-
android:text="@string/app_name"
29-
android:layout_gravity="center_vertical"
30-
android:layout_width="wrap_content"
31-
android:layout_height="wrap_content"/>
31+
android:padding="10dp"
32+
android:text="@string/app_name" />
33+
3234
<TextView
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:layout_gravity="center_vertical"
3338
android:layout_weight="1"
34-
android:padding="10dp"
3539
android:gravity="center"
36-
android:text="@string/app_name"
37-
android:layout_gravity="center_vertical"
38-
android:layout_width="wrap_content"
39-
android:layout_height="wrap_content"/>
40+
android:padding="10dp"
41+
android:text="@string/app_name" />
4042
</com.devzone.fillprogresslayout.FillProgressLayout>
4143

44+
<Button
45+
android:layout_width="wrap_content"
46+
android:layout_height="wrap_content"
47+
android:layout_gravity="center_horizontal"
48+
android:layout_marginTop="20dp"
49+
android:layout_marginBottom="20dp"
50+
android:onClick="toggleFill"
51+
android:text="fill"
52+
tools:ignore="HardcodedText" />
53+
4254
<RelativeLayout
43-
android:layout_margin="20dp"
4455
android:layout_width="match_parent"
45-
android:layout_height="wrap_content">
56+
android:layout_height="wrap_content"
57+
android:layout_margin="20dp">
58+
4659
<com.devzone.fillprogresslayout.FillProgressLayout
60+
android:id="@+id/fillB"
61+
android:layout_width="match_parent"
62+
android:layout_height="wrap_content"
4763
android:layout_alignBottom="@+id/tv"
4864
android:layout_alignParentTop="true"
49-
app:fpl_roundedCornerRadius="20"
50-
app:fpl_isRounded="true"
51-
android:id="@+id/fillB"
52-
app:fpl_gradientMovement="false"
5365
app:fpl_gradientColors="@array/gradientColors"
54-
android:layout_width="match_parent"
55-
android:layout_height="wrap_content"/>
66+
app:fpl_gradientMovement="false"
67+
app:fpl_isRounded="true"
68+
app:fpl_roundedCornerRadius="20" />
5669

5770
<androidx.appcompat.widget.AppCompatTextView
5871
android:id="@+id/tv"
59-
android:text="@string/app_name"
72+
android:layout_width="match_parent"
73+
android:layout_height="wrap_content"
6074
android:gravity="center"
6175
android:padding="20dp"
62-
android:layout_width="match_parent"
63-
android:layout_height="wrap_content"/>
76+
android:text="@string/app_name" />
6477
</RelativeLayout>
6578

66-
<Button
67-
android:layout_width="wrap_content"
79+
<RadioGroup
80+
android:layout_width="match_parent"
6881
android:layout_height="wrap_content"
69-
android:layout_gravity="center_horizontal"
70-
android:layout_marginTop="80dp"
71-
android:onClick="toggleFill"
72-
android:text="fill"
73-
tools:ignore="HardcodedText" />
82+
android:layout_margin="10dp"
83+
android:gravity="center"
84+
android:orientation="horizontal">
85+
86+
<androidx.appcompat.widget.AppCompatCheckBox
87+
android:id="@+id/reverseCB"
88+
android:layout_width="wrap_content"
89+
android:layout_height="wrap_content"
90+
android:layout_margin="5dp"
91+
android:text="Reverse" />
92+
93+
<androidx.appcompat.widget.AppCompatCheckBox
94+
android:id="@+id/animateCB"
95+
android:layout_width="wrap_content"
96+
android:layout_height="wrap_content"
97+
android:layout_margin="5dp"
98+
android:text="Animate" />
99+
</RadioGroup>
100+
101+
102+
<SeekBar
103+
android:id="@+id/progressSeek"
104+
android:layout_width="match_parent"
105+
android:layout_height="wrap_content"
106+
android:layout_margin="10dp" />
107+
108+
74109
</LinearLayout>

fillprogresslayout/src/main/java/com/devzone/fillprogresslayout/FillProgressLayout.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.core.content.ContextCompat
1616
import androidx.core.view.ViewCompat
1717

1818

19+
@Suppress("unused")
1920
class FillProgressLayout : LinearLayout {
2021

2122
companion object {
@@ -252,19 +253,26 @@ class FillProgressLayout : LinearLayout {
252253

253254
//---------------------public setters--------------------------------------------------------------------//
254255

255-
fun setProgress(p: Int) {
256-
if (p in 0..maxProgress) {
256+
fun setProgress(inputProgress: Int,animated:Boolean=true) {
257+
if (inputProgress in 0..maxProgress) {
257258
clearAnimation()
258-
val animator = ValueAnimator.ofInt(oldProgress, p)
259-
animator.interpolator = AccelerateDecelerateInterpolator()
260-
animator.addUpdateListener { anm ->
261-
currentProgress = anm.animatedValue as Int
259+
if(animated) {
260+
val animator = ValueAnimator.ofInt(oldProgress, inputProgress)
261+
animator.interpolator = AccelerateDecelerateInterpolator()
262+
animator.addUpdateListener { anm ->
263+
currentProgress = anm.animatedValue as Int
264+
updateRect(rectF = progressRectF)
265+
ViewCompat.postInvalidateOnAnimation(this)
266+
}
267+
animator.doOnEnd { doOnProgressEnd?.invoke(this); if (!isRestart) oldProgress = inputProgress }
268+
animator.setDuration(((kotlin.math.abs(inputProgress - oldProgress)) * mDurationFactor).toLong())
269+
.start()
270+
}else{
271+
currentProgress = inputProgress
262272
updateRect(rectF = progressRectF)
273+
doOnProgressEnd?.invoke(this)
263274
ViewCompat.postInvalidateOnAnimation(this)
264275
}
265-
animator.doOnEnd { doOnProgressEnd?.invoke(this); if (!isRestart) oldProgress = p }
266-
animator.setDuration(((kotlin.math.abs(p - oldProgress)) * mDurationFactor).toLong())
267-
.start()
268276
}
269277
}
270278

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<enum name="bottom_right_to_top_left" value="6"/>
2828
<enum name="bottom_left_to_top_right" value="7"/>
2929
</attr>
30+
3031
</declare-styleable>
3132

3233
</resources>

0 commit comments

Comments
 (0)