Skip to content

Commit

Permalink
Add SmileyAnimator to handle animation operations
Browse files Browse the repository at this point in the history
- eyes animation done
  • Loading branch information
YuganshT79 committed Jul 14, 2020
1 parent 4386efe commit 5f7fa87
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dev.yuganshtyagi.smileyrating

import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.view.animation.DecelerateInterpolator
import dev.yuganshtyagi.smileyrating.SmileyViewConfig.*

/**
* Created by Yugansh on 15/07/20.
*/
internal class SmileyAnimator(private val config: SmileyViewConfig) {

private var currLeftEyeX: Int = 0
private var currRightEyeX: Int = 0
private var currEyesY: Int = 0

fun updateEyesValues(newPos: EyePos? = null) {
if (newPos == null) {
currLeftEyeX = config.currEyeLX
currRightEyeX = config.currEyeRX
currEyesY = config.currEyeY
} else {
currLeftEyeX = newPos.leftEyeX
currRightEyeX = newPos.rightEyeX
currEyesY = newPos.eyesY
}
}

fun animateEyesToNewPos(invalidate: () -> Unit) {
val newPos = config.getEyePosForState(SmileyState.of(config.defaultRating))
val leftEyeXAnimator = getValueAnimator(currLeftEyeX, newPos.leftEyeX)
leftEyeXAnimator.addUpdateListener {
config.currEyeLX = it.animatedValue as Int
}
val rightEyeXAnimator = getValueAnimator(currRightEyeX, newPos.rightEyeX)
rightEyeXAnimator.addUpdateListener {
config.currEyeRX = it.animatedValue as Int
}
val eyesYAnimator = getValueAnimator(currEyesY, newPos.eyesY)
eyesYAnimator.addUpdateListener {
config.currEyeY = it.animatedValue as Int
invalidate.invoke()
}

val animatorSet = AnimatorSet()
animatorSet.duration = 220L
animatorSet.interpolator = DecelerateInterpolator()
animatorSet.playTogether(leftEyeXAnimator, rightEyeXAnimator, eyesYAnimator)
animatorSet.start()

updateEyesValues(newPos)
}

private fun getValueAnimator(from: Int, to: Int): ValueAnimator {
return ValueAnimator.ofInt(from, to)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import dev.yuganshtyagi.smileyrating.SmileyState.*
*/
internal class SmileyFaceDrawer(context: Context, attributeSet: AttributeSet?) {

private var config: SmileyViewConfig = SmileyViewConfig(context, attributeSet)
private val config: SmileyViewConfig = SmileyViewConfig(context, attributeSet)
private val animator = SmileyAnimator(config)

fun drawFace(canvas: Canvas) {
config.paint.color = config.faceColor
Expand All @@ -25,11 +26,12 @@ internal class SmileyFaceDrawer(context: Context, attributeSet: AttributeSet?) {

fun onMeasure(width: Int, height: Int) {
config.onMeasure(width, height)
animator.updateEyesValues()
}

fun updateRating(rating: Float) {
fun updateRating(rating: Float, invalidate: () -> Unit) {
config.defaultRating = rating.toInt()
config.updateCurrentEyePos()
animator.animateEyesToNewPos(invalidate)
}

private fun drawFaceForState(canvas: Canvas, state: SmileyState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class SmileyRatingView @JvmOverloads constructor(
}

fun setSmiley(rating: Float) {
drawer.updateRating(rating)
invalidate()
drawer.updateRating(rating) { invalidate() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal class SmileyViewConfig(
updateCurrentEyePos()
}

fun updateCurrentEyePos() {
private fun updateCurrentEyePos() {
val position = getEyePosForState(SmileyState.of(defaultRating))
currEyeLX = position.leftEyeX
currEyeRX = position.rightEyeX
Expand Down Expand Up @@ -157,7 +157,7 @@ internal class SmileyViewConfig(
)
}

private fun getEyePosForState(state: SmileyState): EyePos {
fun getEyePosForState(state: SmileyState): EyePos {
return when (state) {
Sad -> {
EyePos(
Expand All @@ -182,9 +182,9 @@ internal class SmileyViewConfig(
}
Happy -> {
EyePos(
leftEyeX = widthCenter - 72.toDp(),
rightEyeX = widthCenter + 72.toDp(),
eyesY = 85.toDp()
leftEyeX = widthCenter - 75.toDp(),
rightEyeX = widthCenter + 75.toDp(),
eyesY = 82.toDp()
)
}
Amazing -> {
Expand All @@ -197,7 +197,7 @@ internal class SmileyViewConfig(
}
}

private data class EyePos(
internal data class EyePos(
var leftEyeX: Int = 0,
val rightEyeX: Int = 0,
val eyesY: Int = 0
Expand Down

0 comments on commit 5f7fa87

Please sign in to comment.