TUGAS 8 PPB - KEYSA ANADEA 5025211028

 

TUGAS 8 PPB

Deskripsi Proyek: Water Bottle

Nama: Keysa Anadea Aqiva Ajie

NRP: 5025211028

Kelas: PPB A

Membuat Aplikasi Visualisasi Botol Air dengan Jetpack Compose

Pada pertemuan ke-8 mata kuliah Pemrograman Perangkat Bergerak, mahasiswa ditugaskan membuat aplikasi Android sederhana menggunakan Jetpack Compose. Aplikasi ini bertujuan untuk memvisualisasikan botol air yang dapat diisi dan dikosongkan sesuai dengan interaksi pengguna.

Tujuan Proyek

Proyek ini bertujuan untuk mengenalkan:

  • Konsep dasar state management di Jetpack Compose.

  • Animasi UI berbasis animateFloatAsState dan animateIntAsState.

  • Struktur aplikasi modular dengan Kotlin.

  • Custom drawing menggunakan Canvas Compose.

Struktur Proyek

Folder app/src/main/java/com/uni/waterbottle/ berisi:

  • MainActivity.kt: Komponen utama yang mengatur tampilan dan interaksi pengguna.

  • WaterBottle.kt: Komponen visualisasi botol yang digambar manual menggunakan Canvas.

File WaterBottle.kt memanfaatkan drawRect dan animasi untuk menunjukkan volume air secara dinamis.

Fitur Utama

  • Tombol Tambah & Kurang Air: Mengubah level air di dalam botol.

  • Animasi Halus: Perubahan volume divisualisasikan dengan transisi animasi.

  • Custom UI: Botol digambar manual, memberikan pengalaman visual yang menarik.

Tampilan Aplikasi

(Tambahkan screenshot jika ada)

Teknologi yang Digunakan

  • Bahasa: Kotlin

  • UI: Jetpack Compose

  • Desain: Material 3

  • Build System: Gradle Kotlin DSL

Pembelajaran

Proyek ini memperkenalkan keterampilan penting dalam pengembangan Android modern, terutama menggunakan paradigma declarative UI. Mahasiswa juga belajar mengelola status aplikasi serta menerapkan animasi dasar.


Waterbottle.kt

package com.uni.waterbottle

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.animateIntAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.clipPath
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun WatterBottle(
modifier: Modifier = Modifier,
totalWaterAmount: Int,
unit: String,
usedWaterAmount: Int,
waterWavesColor: Color = Color(0xff279EFF),
bottleColor: Color = Color.White,
capColor: Color = Color(0xFF0065B9)
) {

val waterPercentage = animateFloatAsState(
targetValue = (usedWaterAmount.toFloat() / totalWaterAmount.toFloat()),
label = "Water Waves animation",
animationSpec = tween(durationMillis = 1000)
).value

val usedWaterAmountAnimation = animateIntAsState(
targetValue = usedWaterAmount,
label = "Used water amount animation",
animationSpec = tween(durationMillis = 1000)
).value

Box(
modifier = modifier
.width(200.dp)
.height(600.dp)
) {

Canvas(modifier = Modifier.fillMaxSize()) {
val width = size.width
val height = size.height

val capWidth = size.width * 0.55f
val capHeight = size.height * 0.13f

//Draw the bottle body
val bodyPath = Path().apply {
moveTo(width * 0.3f, height * 0.1f)
lineTo(width * 0.3f, height * 0.2f)
quadraticBezierTo(
0f, height * 0.3f, // The pulling point
0f, height * 0.4f
)
lineTo(0f, height * 0.95f)
quadraticBezierTo(
0f, height,
width * 0.05f, height
)

lineTo(width * 0.95f, height)
quadraticBezierTo(
width, height,
width, height * 0.95f
)
lineTo(width, height * 0.4f)
quadraticBezierTo(
width, height * 0.3f,
width * 0.7f, height * 0.2f
)
lineTo(width * 0.7f, height * 0.2f)
lineTo(width * 0.7f, height * 0.1f)

close()
}
clipPath(
path = bodyPath
) {
// Draw the color of the bottle
drawRect(
color = bottleColor,
size = size,
topLeft = Offset(0f, 0f)
)

//Draw the water waves
val waterWavesYPosition = (1 - waterPercentage) * size.height

val wavesPath = Path().apply {
moveTo(
x = 0f,
y = waterWavesYPosition
)
lineTo(
x = size.width,
y = waterWavesYPosition
)
lineTo(
x = size.width,
y = size.height
)
lineTo(
x = 0f,
y = size.height
)
close()
}
drawPath(
path = wavesPath,
color = waterWavesColor,
)
}

//Draw the bottle cap
drawRoundRect(
color = capColor,
size = Size(capWidth, capHeight),
topLeft = Offset(size.width / 2 - capWidth / 2f, 0f),
cornerRadius = CornerRadius(45f, 45f)
)


}
val text = buildAnnotatedString {
withStyle(
style = SpanStyle(
color = if (waterPercentage > 0.5f) bottleColor else waterWavesColor,
fontSize = 44.sp
)
) {
append(usedWaterAmountAnimation.toString())
}
withStyle(
style = SpanStyle(
color = if (waterPercentage > 0.5f) bottleColor else waterWavesColor,
fontSize = 22.sp
)
) {
append(" ")
append(unit)
}
}

Box(
modifier = Modifier
.fillMaxSize()
.fillMaxHeight(),
contentAlignment = Alignment.Center
) {
Text(text = text)
}
}
}


@Preview
@Composable
fun WaterBottlePreview() {
WatterBottle(
totalWaterAmount = 2500,
unit = "ml",
usedWaterAmount = 120
)
}

Komentar

Postingan populer dari blog ini

TUGAS 7 PPB - KEYSA ANADEA 5025211028

TUGAS 2 PPB - KEYSA ANADEA 5025211028

TUGAS 9 PPB - KEYSA ANADEA 5025211028