Let's craft a simple, workable code lab based on the course content, specifically focusing on 2D graphics, as it's foundational and aligns with the course's early objectives.
Code Lab: Drawing Basic Shapes on Android
Objective:
Learn to use Canvas and Paint to draw basic 2D shapes (rectangle, circle) on an Android View.
Prerequisites:
Basic understanding of Android Studio and creating layouts (XML or Compose).
Familiarity with Kotlin programming language.
Steps:
Create a Custom View
Create a new Kotlin class (e.g., MyDrawingView) that extends View.
Override the onDraw(canvas: Canvas) method. This is where the drawing magic happens.
Set up Paint
Inside onDraw, create a Paint object.
Configure its properties:
color = Color.BLUE (or any color you like)
style = Paint.Style.FILL (to fill the shape)
You can also set stroke width, style, etc., for outlines.
Draw a Rectangle
Use canvas.drawRect(left, top, right, bottom, paint)
// Draw circle
canvas.drawCircle(200f, 400f, 80f, paint)
}
}
Explanation:
This code lab introduces the core concepts of 2D drawing in Android.
It's simplified for beginners, focusing on drawing basic shapes.
Further exploration could involve:
Handling touch events for interactive drawing.
Drawing more complex shapes (paths, arcs).
Using transformations (translate, rotate, scale).
Animating the drawings.
Remember, this is a basic starting point. The course outline covers more advanced topics like 3D graphics, animations, and utilizing hardware acceleration, which would require more elaborate code labs.
Floating Balloons Game
Let's evolve the basic 2D drawing code into a rudimentary "Floating Balloons" game.
Conceptual Evolution
Balloons:
Represent balloons as circles drawn on the Canvas.
class BalloonGameView(context: Context) : View(context) {
private val paint = Paint().apply {
color = Color.BLUE
style = Paint.Style.FILL
}
private val balloons = mutableListOf<Balloon>()
private var balloonsPopped = 0
private var startTime: Long = 0
init {
// Initialize balloons with random positions and sizes
for (i in 1..10) {
balloons.add(Balloon(
Random.nextInt(width),
Random.nextInt(height),
Random.nextInt(50, 100) // Random radius between 50 and 100
))
}
startTime = SystemClock.uptimeMillis()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
for (balloon in balloons) {
canvas.drawCircle(balloon.x.toFloat(), balloon.y.toFloat(), balloon.radius.toFloat(), paint)
}
}
override fun onTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
val touchX = event.x
val touchY = event.y
for (balloon in balloons) {
if (isTouchInsideBalloon(touchX, touchY, balloon)) {
balloons.remove(balloon)
balloonsPopped++
if (balloonsPopped == 10) {
val endTime = SystemClock.uptimeMillis()
val timeTaken = (endTime - startTime) / 1000.0 // Time in seconds
// Display timeTaken (you'll need to implement this part)
}
invalidate() // Redraw the view
break
}
}
}
return true
}