Created
September 16, 2017 09:53
-
-
Save TakuSemba/465a9463378b95d2f1bd987dc07e0957 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 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