Skip to content

Instantly share code, notes, and snippets.

@TakuSemba
Created September 16, 2017 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakuSemba/24faa8ed295fe5a30c2f4ede6175c80e to your computer and use it in GitHub Desktop.
Save TakuSemba/24faa8ed295fe5a30c2f4ede6175c80e to your computer and use it in GitHub Desktop.
class SpringRotationView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
private companion object Params {
val INITIAL_ROTATION = 0f
val STIFFNESS = SpringForce.STIFFNESS_MEDIUM
val DAMPING_RATIO = SpringForce.DAMPING_RATIO_HIGH_BOUNCY
}
private val rotationAnim: SpringAnimation = SpringAnimation(this, SpringAnimation.ROTATION).apply {
spring = SpringForce(INITIAL_ROTATION).apply {
stiffness = STIFFNESS
dampingRatio = DAMPING_RATIO
}
}
init {
var currentRotation = 0f
setOnTouchListener { view, event ->
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
rotationAnim.cancel()
currentRotation = rotationAmount(event.x, event.y)
}
MotionEvent.ACTION_MOVE -> {
val previousRotation = currentRotation
currentRotation = rotationAmount(event.x, event.y)
view.rotation += currentRotation - previousRotation
}
MotionEvent.ACTION_UP -> rotationAnim.start()
}
true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment