Skip to content

Instantly share code, notes, and snippets.

View sagar-viradiya's full-sized avatar
:octocat:
Focusing

Sagar Viradiya sagar-viradiya

:octocat:
Focusing
View GitHub Profile
@sagar-viradiya
sagar-viradiya / SpringAnimation.kt
Created August 15, 2017 09:31
Creating SpringAnimation and attaching SpringForce
private val springForce: SpringForce by lazy(LazyThreadSafetyMode.NONE) {
SpringForce(0f).apply {
stiffness = SpringForce.STIFFNESS_MEDIUM
dampingRatio = SpringForce.DAMPING_RATIO_HIGH_BOUNCY
}
}
private val springAnimationTranslationX: SpringAnimation by lazy(LazyThreadSafetyMode.NONE) {
SpringAnimation(android_bot, DynamicAnimation.TRANSLATION_X).setSpring(springForce)
}
private fun setupTouchListener() {
android_bot.setOnTouchListener { view, motionEvent ->
when(motionEvent?.action) {
MotionEvent.ACTION_DOWN -> {
xDiffInTouchPointAndViewTopLeftCorner = motionEvent.rawX - view.x
yDiffInTouchPointAndViewTopLeftCorner = motionEvent.rawY - view.y
val flingAnimationX: FlingAnimation by lazy(LazyThreadSafetyMode.NONE) {
FlingAnimation(android_bot, DynamicAnimation.X).setFriction(1.1f)
}
val flingAnimationY: FlingAnimation by lazy(LazyThreadSafetyMode.NONE) {
FlingAnimation(android_bot, DynamicAnimation.Y).setFriction(1.1f)
}
val gestureListener = object : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent?): Boolean {
return true
}
override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
flingAnimationX.setStartVelocity(velocityX)
flingAnimationY.setStartVelocity(velocityY)
android_bot.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
flingAnimationX.setMinValue(0f).setMaxValue((screenSize.x - android_bot.width).toFloat())
flingAnimationY.setMinValue(0f).setMaxValue((screenSize.y - android_bot.height).toFloat())
android_bot.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
val firstSpringAnimationX by lazy(LazyThreadSafetyMode.NONE) {
createSpringAnimation(android_bot1, DynamicAnimation.TRANSLATION_X)
}
val firstSpringAnimationY by lazy(LazyThreadSafetyMode.NONE) {
createSpringAnimation(android_bot1, DynamicAnimation.TRANSLATION_Y)
}
val secondSpringAnimationX by lazy(LazyThreadSafetyMode.NONE) {
createSpringAnimation(android_bot2, DynamicAnimation.TRANSLATION_X)
private fun setupOnTouchListener() {
...
android_bot.setOnTouchListener { view, motionEvent ->
if(motionEvent.action == MotionEvent.ACTION_MOVE) {
val deltaX = motionEvent.rawX - lastX
val deltaY = motionEvent.rawY - lastY
firstSpringAnimationX.addUpdateListener { _, value, _ ->
secondSpringAnimationX.animateToFinalPosition(value)
}
firstSpringAnimationY.addUpdateListener { _, value, _ ->
secondSpringAnimationY.animateToFinalPosition(value)
}
@sagar-viradiya
sagar-viradiya / MaximumSubArray.kt
Last active March 17, 2018 11:51
Solution of maximum sub array problem in kotlin
/**
* Maximum sub array problem
*
* Given an array of integers find the sub array whose sum is maximum
*/
fun main(args: Array<String>) {
val array = intArrayOf(13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7)
print(getMaximumSubArray(array, 0, array.size - 1))
@sagar-viradiya
sagar-viradiya / HeapSort.kt
Created March 17, 2018 11:42
Heap sort in kotlin
/**
* Heap sort
*
* Given an array, sort it in ascending order using heap sort algorithm
*/
fun main(args: Array<String>) {
val array = intArrayOf(4, 1, 3, 2, 16, 9, 10, 14, 8, 7)