Created
September 16, 2017 09:53
-
-
Save TakuSemba/24faa8ed295fe5a30c2f4ede6175c80e to your computer and use it in GitHub Desktop.
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
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