MainActivity.kt
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.animation.core.*
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainContent()
}
}
@Composable
fun MainContent(){
val isOpaque = remember { mutableStateOf(value = true) }
val isEnabled = remember { mutableStateOf(true)}
// Repeatable runs a duration based animation.
// Animate repeatedly until it reaches the specified iteration count.
val alpha by animateFloatAsState(
targetValue = if (isOpaque.value) 1.0f else 0.3f,
animationSpec = repeatable(
iterations = 7, // iteration count
animation = tween(durationMillis = 1000),
repeatMode = RepeatMode.Reverse
),
finishedListener = {
isEnabled.value = true
}
)
Column(
modifier = Modifier
.fillMaxSize()
.background(Color(0xFFEDC9AF))
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
onClick = {
isOpaque.value = !isOpaque.value
isEnabled.value = false
},
colors = ButtonDefaults.buttonColors(
Color(0xFF6C541E), Color(0xCCFFFFFF)
),
enabled = isEnabled.value
) {
Text(
text = "Animate Opacity",
modifier = Modifier.padding(12.dp)
)
}
Icon(
Icons.Filled.Favorite,
"Localized description",
tint = Color(0xFFCE2029),
modifier = Modifier
.size(300.dp)
.alpha(alpha = alpha)
)
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
- jetpack compose - Text custom font
- jetpack compose - Accessing font resource
- jetpack compose - LazyColumn scroll to top bottom
- jetpack compose - LazyColumn smooth scrolling
- jetpack compose - LazyColumn scroll to position
- jetpack compose - Animate content size
- jetpack compose - FadeIn FadeOut animation
- jetpack compose - ExpandIn ShrinkOut animation
- jetpack compose - Animation duration
- jetpack compose - animateFloatAsState
- jetpack compose - Animation finished listener
- jetpack compose - Snap animation
- jetpack compose - Combine multiple transitions
- jetpack compose - AnimateContentSize customization
- jetpack compose - Spring animation