Skip to content

Commit

Permalink
Add sample for progress animation with percent value
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Nasrabadi committed Apr 6, 2023
1 parent db6fafd commit 7afec69
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -26,6 +27,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay

class MainActivity : ComponentActivity() {

Expand All @@ -39,13 +41,25 @@ class MainActivity : ComponentActivity() {

@Composable
private fun HomeScreen() {
var percent by remember { mutableStateOf(0f) }
LaunchedEffect(Unit) {
while (percent < 100) {
delay(500)
percent += 1
}
}

Column(Modifier.fillMaxSize()) {
//animation with duration sample
ShapeWithRoundedCorner()
RectangleShape()
CircleShape()
PolygonShape()
// progress sample
PolygonShapeWithProgress(percent)
}
}

@Composable
private fun ShapeWithRoundedCorner() {
var logText by remember { mutableStateOf("Rounded corner animation started!") }
Expand Down Expand Up @@ -101,7 +115,8 @@ private fun PolygonShape() {
var logText by remember { mutableStateOf("Polygon Animation Started!") }
Box(
Modifier
.fillMaxSize()
.fillMaxWidth()
.height(68.dp)
.padding(16.dp)
.clip(CutCornerShape(96.dp))
.progressAnimation(durationMillis = 4000, finishedListener = {
Expand All @@ -114,6 +129,23 @@ private fun PolygonShape() {
}
}

@Composable
private fun PolygonShapeWithProgress(percent: Float) {
val logText by remember { mutableStateOf("Polygon Animation With progress Started!") }
Box(
Modifier
.fillMaxWidth()
.height(68.dp)
.padding(16.dp)
.clip(CutCornerShape(96.dp))
.progressAnimation(percent = percent)
.background(Color.Yellow),
contentAlignment = Alignment.Center
) {
FinishedText(text = logText, textColor = Color.Black)
}
}

@Composable
private fun FinishedText(text: String, textColor: Color? = null) {
val textColorBasedOnTheme = if (isSystemInDarkTheme()) {
Expand Down

0 comments on commit 7afec69

Please sign in to comment.