-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SmileyAnimator to handle animation operations
- eyes animation done
- Loading branch information
1 parent
4386efe
commit 5f7fa87
Showing
4 changed files
with
69 additions
and
11 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
smileyrating/src/main/java/dev/yuganshtyagi/smileyrating/SmileyAnimator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters