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/465a9463378b95d2f1bd987dc07e0957 to your computer and use it in GitHub Desktop.
Save TakuSemba/465a9463378b95d2f1bd987dc07e0957 to your computer and use it in GitHub Desktop.
class SpringScaleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
private companion object Params {
val INITIAL_SCALE = 1f
val STIFFNESS = SpringForce.STIFFNESS_MEDIUM
val DAMPING_RATIO = SpringForce.DAMPING_RATIO_HIGH_BOUNCY
}
private val xAnim: SpringAnimation = SpringAnimation(this, SpringAnimation.SCALE_X).apply {
spring = SpringForce(INITIAL_SCALE).apply {
stiffness = STIFFNESS
dampingRatio = DAMPING_RATIO
}
}
private val yAnim: SpringAnimation = SpringAnimation(this, SpringAnimation.SCROLL_Y).apply {
spring = SpringForce(INITIAL_SCALE).apply {
stiffness = STIFFNESS
dampingRatio = DAMPING_RATIO
}
}
private val detector = ScaleGestureDetector(context,
object:ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(detector: ScaleGestureDetector): Boolean {
var scaleFactor = 1f
scaleFactor *= detector.scaleFactor
scaleX *= scaleFactor
scaleY *= scaleFactor
return true
}
})
init {
setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_UP) {
xAnim.start()
yAnim.start()
} else {
xAnim.cancel()
yAnim.cancel()
detector.onTouchEvent(event)
}
true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment