Created
September 21, 2018 17:58
-
-
Save ar-android/6f24b713968f78539239b03519f5f09c to your computer and use it in GitHub Desktop.
Game Block Animation Move To Left
This file contains hidden or 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 paintBlock = Paint().apply { | |
style = Paint.Style.STROKE | |
color = Color.BLUE | |
strokeWidth = 220F | |
} | |
var position_x_block = (0F - 100F) | |
var random_y = 0 | |
private fun drawGameBlock(canvas: Canvas?) { | |
if (position_x_block == (0F - 100F)) { | |
random_y = (200..270).shuffled().last() | |
position_x_block = (width + 100).toFloat() | |
moveLeft() | |
} | |
val startX_1 = position_x_block | |
val startY_1 = (height / 2 - random_y).toFloat() | |
val stopX_1 = startX_1 | |
val stopY_1 = 0F | |
canvas?.drawLine(startX_1, startY_1, stopX_1, stopY_1, paintBlock) | |
canvas?.drawLine(startX_1, startY_1 + (random_y * 2).toFloat(), stopX_1, height.toFloat(), paintBlock) | |
} | |
private fun moveLeft() { | |
val moveLeft = ValueAnimator.ofFloat(position_x_block, (0F - 100F)) | |
with(moveLeft) { | |
duration = 2400 | |
interpolator = AccelerateDecelerateInterpolator() | |
addUpdateListener { | |
position_x_block = it.animatedValue as Float | |
invalidate() | |
} | |
addListener(object : AnimatorListenerAdapter() { | |
override fun onAnimationEnd(animation: Animator?) { | |
random_y = (200..270).shuffled().last() | |
} | |
}) | |
start() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment