Skip to content

Commit

Permalink
Adds slide left/right animation and fixes bug with exit animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aritra Roy committed Apr 18, 2018
1 parent 263fa25 commit 184d564
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 16 deletions.
14 changes: 10 additions & 4 deletions app/src/main/java/com/andrognito/flashbardemo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ class MainActivity : AppCompatActivity() {

show.setOnClickListener {
if (flashbar == null) {

flashbar = Flashbar.Builder(activity)
.gravity(Flashbar.Gravity.BOTTOM)
.gravity(Flashbar.Gravity.TOP)
.backgroundColorRes(R.color.colorPrimaryDark)
.title("Hello!")
.message("Loading, please wait...")
Expand All @@ -35,8 +34,15 @@ class MainActivity : AppCompatActivity() {
})
.enterAnimation(FlashAnim.with(this)
.duration(450)
.slideFromLeft()
.alpha()
.overshoot())
.exitAnimation(FlashAnim.with(this)
.duration(450)
.slideFromLeft()
.alpha()
.overshoot())
.dismissOnTapOutside()
//.dismissOnTapOutside()
.showIcon(true)
.enableSwipeToDismiss()
.positiveActionText("OKAY")
Expand Down Expand Up @@ -88,6 +94,7 @@ class MainActivity : AppCompatActivity() {

override fun onDismissed(bar: Flashbar, event: Flashbar.DismissEvent) {
Log.d("Flashbar", "onDismissed: $event")
flashbar = null
}
})
//.showIcon(true)
Expand All @@ -98,7 +105,6 @@ class MainActivity : AppCompatActivity() {

dismiss.setOnClickListener {
flashbar?.dismiss()
flashbar = null
}
}
}
80 changes: 71 additions & 9 deletions flashbar/src/main/java/com/andrognito/flashbar/FlashAnimBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.animation.ObjectAnimator
import android.content.Context
import android.support.annotation.InterpolatorRes
import android.view.animation.*
import com.andrognito.flashbar.FlashAnimBuilder.Direction.LEFT
import com.andrognito.flashbar.FlashAnimBuilder.Direction.RIGHT
import com.andrognito.flashbar.FlashAnimBuilder.Type.ENTER
import com.andrognito.flashbar.FlashAnimBuilder.Type.EXIT
import com.andrognito.flashbar.Flashbar.Gravity.BOTTOM
Expand All @@ -22,40 +24,83 @@ class FlashAnimBuilder(private val context: Context) {
private var gravity: Flashbar.Gravity? = null
private var interpolator: Interpolator? = null
private var alpha: Boolean = false
private var direction: Direction? = null

/**
* Specifies the duration for enter/exit animation
* By default, the duration is 250 ms
*/
fun duration(millis: Long) = apply {
require(duration >= 0, { "Duration must not be negative" })
this.duration = millis
}

/**
* Slides bar from left as enter/exit animation
*/
fun slideFromLeft() = apply {
this.direction = LEFT
}

/**
* Slides bar from right as enter/exit animation
*/
fun slideFromRight() = apply {
this.direction = RIGHT
}

/**
* Adds accelerate interpolator to the enter/exit animation
*/
fun accelerate() = apply {
this.interpolator = AccelerateInterpolator()
}

/**
* Adds decelerate interpolator to the enter/exit animation
*/
fun decelerate() = apply {
this.interpolator = DecelerateInterpolator()
}

/**
* Adds accelerateDecelerate interpolator to the enter/exit animation
*/
fun accelerateDecelerate() = apply {
this.interpolator = AccelerateDecelerateInterpolator()
}

/**
* Adds overshoot interpolator to the enter/exit animation
*/
fun overshoot() = apply {
this.interpolator = OvershootInterpolator()
}

/**
* Adds anticipateOvershoot interpolator to the enter/exit animation
*/
fun anticipateOvershoot() = apply {
this.interpolator = AnticipateInterpolator()
}

/**
* Adds custom interpolator to the enter/exit animation
*/
fun interpolator(interpolator: Interpolator) = apply {
this.interpolator = interpolator
}

/**
* Adds custom interpolator resource to the enter/exit animation
*/
fun interpolator(@InterpolatorRes id: Int) = apply {
this.interpolator = AnimationUtils.loadInterpolator(context, id)
}

/**
* Adds alpha transition to the enter/exit animation
*/
fun alpha() = apply {
this.alpha = true
}
Expand All @@ -80,20 +125,36 @@ class FlashAnimBuilder(private val context: Context) {
val compositeAnim = AnimatorSet()

val translationAnim = ObjectAnimator()
translationAnim.propertyName = "translationY"
translationAnim.target = view
if (direction == null) {
translationAnim.propertyName = "translationY"

when (type!!) {
ENTER -> when (gravity!!) {
TOP -> translationAnim.setFloatValues(-view.height.toFloat(), 0f)
BOTTOM -> translationAnim.setFloatValues(view.height.toFloat(), 0f)
when (type!!) {
ENTER -> when (gravity!!) {
TOP -> translationAnim.setFloatValues(-view.height.toFloat(), 0f)
BOTTOM -> translationAnim.setFloatValues(view.height.toFloat(), 0f)
}
EXIT -> when (gravity!!) {
TOP -> translationAnim.setFloatValues(0f, -view.height.toFloat())
BOTTOM -> translationAnim.setFloatValues(0f, view.height.toFloat())
}
}
EXIT -> when (gravity!!) {
TOP -> translationAnim.setFloatValues(0f, -view.height.toFloat())
BOTTOM -> translationAnim.setFloatValues(0f, view.height.toFloat())
} else {
translationAnim.propertyName = "translationX"

when (type!!) {
ENTER -> when (direction!!) {
LEFT -> translationAnim.setFloatValues(-view.width.toFloat(), 0f)
RIGHT -> translationAnim.setFloatValues(view.width.toFloat(), 0f)
}
EXIT -> when (direction!!) {
LEFT -> translationAnim.setFloatValues(0f, -view.width.toFloat())
RIGHT -> translationAnim.setFloatValues(0f, view.width.toFloat())
}
}
}

translationAnim.target = view

val alphaAnim = ObjectAnimator()
alphaAnim.propertyName = "alpha"
alphaAnim.target = view
Expand All @@ -113,4 +174,5 @@ class FlashAnimBuilder(private val context: Context) {
}

enum class Type { ENTER, EXIT }
enum class Direction { LEFT, RIGHT }
}
6 changes: 3 additions & 3 deletions flashbar/src/main/java/com/andrognito/flashbar/Flashbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class Flashbar private constructor(private var builder: Builder) {
* Specifies the exit animation of the flashbar
*/
fun exitAnimation(builder: FlashAnimBuilder) = apply {
this.enterAnimBuilder = builder
this.exitAnimBuilder = builder
}

/**
Expand Down Expand Up @@ -744,8 +744,8 @@ class Flashbar private constructor(private var builder: Builder) {
}
} else {
when (gravity) {
TOP -> enterAnimBuilder!!.exit().fromTop()
BOTTOM -> enterAnimBuilder!!.exit().fromBottom()
TOP -> exitAnimBuilder!!.exit().fromTop()
BOTTOM -> exitAnimBuilder!!.exit().fromBottom()
}
}
}
Expand Down

0 comments on commit 184d564

Please sign in to comment.