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
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) | |
} |
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
private fun setupTouchListener() { | |
android_bot.setOnTouchListener { view, motionEvent -> | |
when(motionEvent?.action) { | |
MotionEvent.ACTION_DOWN -> { | |
xDiffInTouchPointAndViewTopLeftCorner = motionEvent.rawX - view.x | |
yDiffInTouchPointAndViewTopLeftCorner = motionEvent.rawY - view.y |
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
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) | |
} |
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
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) |
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
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) | |
} | |
}) |
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
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) |
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
private fun setupOnTouchListener() { | |
... | |
android_bot.setOnTouchListener { view, motionEvent -> | |
if(motionEvent.action == MotionEvent.ACTION_MOVE) { | |
val deltaX = motionEvent.rawX - lastX | |
val deltaY = motionEvent.rawY - lastY |
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
firstSpringAnimationX.addUpdateListener { _, value, _ -> | |
secondSpringAnimationX.animateToFinalPosition(value) | |
} | |
firstSpringAnimationY.addUpdateListener { _, value, _ -> | |
secondSpringAnimationY.animateToFinalPosition(value) | |
} |
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
/** | |
* 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) |
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
/** | |
* 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)) |
OlderNewer