MainActivity.kt
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ColorLens
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt
class MainActivity : AppCompatActivity() {
@ExperimentalMaterialApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainContent()
}
}
@ExperimentalMaterialApi
@Composable
fun MainContent(){
var bgColor:Color by remember {
mutableStateOf(Color(0xFF88540B))
}
val color = animateColorAsState(
targetValue = bgColor,
animationSpec = tween(
durationMillis = 2000
)
)
val squareSize = 150.dp
val swipeAbleState = rememberSwipeableState(0)
val sizePx = with(LocalDensity.current) { squareSize.toPx() }
val anchors = mapOf(0f to 0, sizePx to 1)
Column(
modifier = Modifier
.background(Color(0xFFEDEAE0))
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(16.dp))
.background(Color(0xFFBFAFB2))
.padding(8.dp)
.swipeable(
state = swipeAbleState,
anchors = anchors,
thresholds = { _, _ ->
FractionalThreshold(0.3f)
},
orientation = Orientation.Horizontal
)
) {
Column(
modifier = Modifier.padding(16.dp)
) {
IconButton(onClick = {
bgColor = Color(0xFF2A52BE)
}) {
Icon(
Icons.Filled.ColorLens,
contentDescription = "Localized description",
tint = Color(0xFF2A52BE)
)
}
IconButton(onClick = {
bgColor = Color(0xFFE30022)
}) {
Icon(
Icons.Filled.ColorLens,
contentDescription = "Localized description",
tint = Color(0xFFE30022)
)
}
}
Box(
Modifier
.offset {
IntOffset(
swipeAbleState.offset.value.roundToInt(),
0
)
}
.clip(RoundedCornerShape(16.dp))
.fillMaxWidth(0.95F)
.height(150.dp)
.background(color.value)
)
}
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
- jetpack compose - Navigation drawer example
- jetpack compose - Cut corner shape example
- jetpack compose - Image from drawable
- jetpack compose - Image clickable
- jetpack compose - Image border
- jetpack compose - Image shape
- jetpack compose - Image tint
- jetpack compose - Image from assets folder
- jetpack compose - How to use LazyColumn
- jetpack compose - Accessing string resources
- jetpack compose - String resource positional formatting
- jetpack compose - Dragging
- jetpack compose - Multiple draggable objects
- jetpack compose - Panning zooming rotating
- jetpack compose - Weight modifier