Skip to content

Commit

Permalink
Merge pull request #10 from JDevZone/dev
Browse files Browse the repository at this point in the history
optional progress animation
  • Loading branch information
JDevZone authored Dec 16, 2019
2 parents e1ba357 + 9d8b890 commit f91baf6
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 55 deletions.
49 changes: 42 additions & 7 deletions app/src/main/java/com/devzone/fpl_sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.devzone.fpl_sample
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

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

}

private fun handlePreciseControls() {
progressSeek?.apply {
max = 100
setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(
seekBar: SeekBar?,
progress: Int,
fromUser: Boolean
) {
updateLargeProgress(progress)
}

override fun onStartTrackingTouch(seekBar: SeekBar?) {

}

override fun onStopTrackingTouch(seekBar: SeekBar?) {

}

})
}

}

private fun updateLargeProgress(progress: Int) {
val isAnimated = animateCB.isChecked
val isReverse = reverseCB.isChecked

if (isReverse) {
val reverseProgress = kotlin.math.abs(100 - progress)
fillB.setProgress(reverseProgress, isAnimated)
} else
fillB.setProgress(progress, isAnimated)
}

fun toggleFill(view: View) {
val button: Button = view as Button
button.isEnabled = false

isFilled = !isFilled
fillL.setProgress(if (isFilled) 100 else 0)
fillB.setProgress(if (isFilled) 100 else 0)
fillL.setProgressColors(intArrayOf(R.color.colorGradient1,R.color.colorGradient2))
fillB.setDoOnProgressEnd { v ->
button.isEnabled = true;button.text = if (isFilled) "Unfill" else "Fill"
fillL?.setDoOnProgressEnd { v ->
button.isEnabled = true;button.text = if (isFilled) "UnFill" else "Fill"
}

fillL?.setProgress(if (isFilled) 100 else 0, false)
fillL?.setProgressColors(intArrayOf(R.color.colorGradient1, R.color.colorGradient2))
}
}
113 changes: 74 additions & 39 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,74 +1,109 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<com.devzone.fillprogresslayout.FillProgressLayout
android:id="@+id/fillL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
app:fpl_backgroundColor="@color/colorRedTrans"
app:fpl_progressColor="@color/colorGreenTrans"
app:fpl_gradientMovement="true"
app:fpl_isRounded="true"
app:fpl_roundedCornerRadius="100"
app:fpl_progress="0"
app:fpl_gradientMovement="true"
app:fpl_progressDuration="2000"
app:fpl_progressColor="@color/colorGreenTrans"
app:fpl_progressDirection="left_to_right"
app:fpl_shouldRestart="false"
android:layout_width="match_parent"
android:layout_height="wrap_content">
app:fpl_progressDuration="2000"
app:fpl_roundedCornerRadius="100"
app:fpl_shouldRestart="false">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="@string/app_name"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:padding="10dp"
android:text="@string/app_name" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="@string/app_name"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:padding="10dp"
android:text="@string/app_name" />
</com.devzone.fillprogresslayout.FillProgressLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:onClick="toggleFill"
android:text="fill"
tools:ignore="HardcodedText" />

<RelativeLayout
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:layout_margin="20dp">

<com.devzone.fillprogresslayout.FillProgressLayout
android:id="@+id/fillB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv"
android:layout_alignParentTop="true"
app:fpl_roundedCornerRadius="20"
app:fpl_isRounded="true"
android:id="@+id/fillB"
app:fpl_gradientMovement="false"
app:fpl_gradientColors="@array/gradientColors"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
app:fpl_gradientMovement="false"
app:fpl_isRounded="true"
app:fpl_roundedCornerRadius="20" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv"
android:text="@string/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:text="@string/app_name" />
</RelativeLayout>

<Button
android:layout_width="wrap_content"
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="80dp"
android:onClick="toggleFill"
android:text="fill"
tools:ignore="HardcodedText" />
android:layout_margin="10dp"
android:gravity="center"
android:orientation="horizontal">

<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/reverseCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Reverse" />

<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/animateCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Animate" />
</RadioGroup>


<SeekBar
android:id="@+id/progressSeek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />


</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat


@Suppress("unused")
class FillProgressLayout : LinearLayout {

companion object {
Expand Down Expand Up @@ -252,19 +253,26 @@ class FillProgressLayout : LinearLayout {

//---------------------public setters--------------------------------------------------------------------//

fun setProgress(p: Int) {
if (p in 0..maxProgress) {
fun setProgress(inputProgress: Int,animated:Boolean=true) {
if (inputProgress in 0..maxProgress) {
clearAnimation()
val animator = ValueAnimator.ofInt(oldProgress, p)
animator.interpolator = AccelerateDecelerateInterpolator()
animator.addUpdateListener { anm ->
currentProgress = anm.animatedValue as Int
if(animated) {
val animator = ValueAnimator.ofInt(oldProgress, inputProgress)
animator.interpolator = AccelerateDecelerateInterpolator()
animator.addUpdateListener { anm ->
currentProgress = anm.animatedValue as Int
updateRect(rectF = progressRectF)
ViewCompat.postInvalidateOnAnimation(this)
}
animator.doOnEnd { doOnProgressEnd?.invoke(this); if (!isRestart) oldProgress = inputProgress }
animator.setDuration(((kotlin.math.abs(inputProgress - oldProgress)) * mDurationFactor).toLong())
.start()
}else{
currentProgress = inputProgress
updateRect(rectF = progressRectF)
doOnProgressEnd?.invoke(this)
ViewCompat.postInvalidateOnAnimation(this)
}
animator.doOnEnd { doOnProgressEnd?.invoke(this); if (!isRestart) oldProgress = p }
animator.setDuration(((kotlin.math.abs(p - oldProgress)) * mDurationFactor).toLong())
.start()
}
}

Expand Down
1 change: 1 addition & 0 deletions fillprogresslayout/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<enum name="bottom_right_to_top_left" value="6"/>
<enum name="bottom_left_to_top_right" value="7"/>
</attr>

</declare-styleable>

</resources>

0 comments on commit f91baf6

Please sign in to comment.