Last active
June 16, 2021 13:59
-
-
Save maozgal/84d7afadf0487bd339a8070308826219 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 MainActivity : AppCompatActivity() { | |
private var mostHighDashedLine: View? = null | |
private var dashArray = arrayListOf<View>() | |
private var dashLinesCount = 0; | |
private var timer = Timer() | |
private var height = 100 | |
private var dashLineHeight = 400 | |
private var dashLineWidth = 100 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
gameInit() | |
} | |
private fun runGame() { | |
timer.cancel() | |
timer = Timer() | |
timer.scheduleAtFixedRate(object : TimerTask() { | |
override fun run() { | |
gameLoop() | |
} | |
}, 0L, 3L); | |
} | |
private fun gameLoop() { | |
// Moving UI elements has to be done by the main thread. | |
runOnUiThread { | |
// Run through all the dashed lines and increase their Y-axis parameter by 2 px. | |
for (i in 0..dashLinesCount) { | |
var dashView = dashArray.get(i); | |
dashView.y += 2; | |
// If a dashed line is out of sight (below the bottom of the screen), reset it to be | |
// on top of the highest dashed line. | |
if (dashView.y > height + (dashLineHeight / 2)) { | |
val mostHighDashedLineY:Float = (mostHighDashedLine?.y ?: 0F) | |
dashView.y = mostHighDashedLineY - (2 * dashLineHeight.toFloat()) | |
mostHighDashedLine = dashView | |
} | |
} | |
} | |
} | |
private fun gameInit() { | |
initValues() | |
} | |
private fun initValues() { | |
val root = findViewById<ConstraintLayout>(R.id.root) | |
root.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { | |
override fun onGlobalLayout() { | |
root.viewTreeObserver.removeOnGlobalLayoutListener(this) | |
val displayMetrics = DisplayMetrics() | |
windowManager.defaultDisplay.getMetrics(displayMetrics) | |
height = displayMetrics.heightPixels | |
dashLinesCount = height / dashLineHeight | |
buildDashedLine() | |
runGame() | |
} | |
}) | |
} | |
private fun buildDashedLine() { | |
// Take the root view (Layout) | |
val root = findViewById<ConstraintLayout>(R.id.root) | |
// Build the first dash line. | |
val bottomLine = buildDashLineView() | |
root.addView(bottomLine, 0) | |
attachViewToBottomOfParent(root, bottomLine) | |
dashArray.add(bottomLine) | |
// Build the rest of the the dash lines. | |
var currentV = bottomLine | |
for (i in 0 until dashLinesCount) { | |
val v = buildDashLineView() | |
root.addView(v) | |
attachViewToTopOfOtherView(root, v, currentV) | |
// Remember the current dashed line so the constraints could be on top of it | |
currentV = v | |
dashArray.add(currentV) | |
// The current most high dashed line. needed for when resetting dashed line from bottom | |
// to top, this will be the dashed line to make the constraint upon | |
mostHighDashedLine = v | |
} | |
} | |
private fun attachViewToTopOfOtherView(root: ConstraintLayout?, view: View, other: View) { | |
val constraints = ConstraintSet() | |
generateBasicDashConstraints(constraints, root, view) | |
constraints.connect(view.id, ConstraintSet.BOTTOM, other.id, ConstraintSet.TOP, dashLineHeight) | |
constraints.applyTo(root) | |
} | |
private fun attachViewToBottomOfParent(root: ConstraintLayout?, view: View) { | |
val constraints = ConstraintSet() | |
generateBasicDashConstraints(constraints, root, view) | |
constraints.connect(view.id, ConstraintSet.BOTTOM, R.id.root, ConstraintSet.BOTTOM,dashLineHeight) | |
view.setBackgroundColor(Color.WHITE) | |
constraints.applyTo(root) | |
} | |
private fun generateBasicDashConstraints( | |
constraints: ConstraintSet, | |
root: ConstraintLayout?, | |
view: View | |
) { | |
constraints.clone(root) | |
constraints.constrainHeight(view.id, dashLineHeight) | |
constraints.constrainWidth(view.id, dashLineWidth) | |
constraints.connect(view.id, ConstraintSet.LEFT, R.id.root, ConstraintSet.LEFT) | |
constraints.connect(view.id, ConstraintSet.RIGHT, R.id.root, ConstraintSet.RIGHT) | |
} | |
private fun buildDashLineView(): View { | |
val v = View(this) | |
v.id = View.generateViewId() | |
v.setBackgroundColor(Color.WHITE) | |
return v | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment