我尝试了 Jetpack Compose 中的动画。我在旋转动画中遇到问题。
看起来一切都很好。但不知道为什么它不起作用。
我的代码:
@Composable
private fun RotateAnimationContent() {
val isRotated = rememberSaveable { mutableStateOf(false) }
val rotationAngle by animateFloatAsState(
targetValue = if (isRotated.value) 360F else 0F,
animationSpec = tween(durationMillis = 1500,easing = FastOutLinearInEasing)
)
Column {
Box(modifier = Modifier.background(Color.Red).size(100.dp).rotate(rotationAngle))
Button(
onClick = { isRotated.value = !isRotated.value },
modifier = Modifier.padding(10.dp)
) {
Text(text = "Rotate Box")
}
}
}
Run Code Online (Sandbox Code Playgroud) 下图显示了我想要实现的目标。我希望选项卡指示器是一个短圆角条。
我查了一下 的实现TabRowDefaults.Indicator(),然后做了我自己的一个。我只是尝试添加clip()修改器,但没有成功。我尝试更改修饰符的顺序,但仍然没有成功。
这是我的代码实现:
@Composable
fun TabLayout(
tabItems: List<String>,
content: @Composable () -> Unit
) {
var tabIndex by remember { mutableStateOf(0) }
Column {
ScrollableTabRow(
selectedTabIndex = tabIndex,
edgePadding = 0.dp,
backgroundColor = MaterialTheme.colors.background,
contentColor = Blue100,
indicator = { tabPositions ->
Box(
modifier = Modifier
.tabIndicatorOffset(tabPositions[tabIndex])
.height(4.dp)
.clip(RoundedCornerShape(8.dp)) // clip modifier not working
.padding(horizontal = 28.dp)
.background(color = AnkiBlue100)
)
},
divider = {},
) {
tabItems.forEachIndexed { index, item ->
Tab(
selected = …Run Code Online (Sandbox Code Playgroud) I am building an Android App which needs to create several vertical sliders in the same page for music equalizer adjustment, but I can only find horizontal sliders from the official material design documents.
I try to implement default slider from official documents and rotate it with modifier and it works, but the problem is that I am not able to adjust the height now using Modifier.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent …Run Code Online (Sandbox Code Playgroud) Android Jetpack Compose 包含width(),height()和size()布局修饰符以及requiredWidth(),requiredHeight()和requiredSize()。这两组修饰符有什么区别?我应该使用普通修饰符还是必需的修饰符?
android android-layout android-jetpack android-jetpack-compose
我在 Compose 中有一个“简单”布局,其中 aColumn有两个元素:
我希望布局具有以下行为:
screenWidth200.dp这意味着如果方块中的文本很短,则图像将覆盖顶部的大方块。但如果任何方形文本真的很长,我希望整个网格放大和缩小图像。这些是理想的结果:


我已经ConstraintLayout在 Compose 中尝试过此操作,但无法使正方形正确缩放。
使用 a 时Column,我无法获得随大内容一起增长的选项 - 文本只是被截断,而图像仍然是一个巨大的正方形。
这些是我构建的组件:
// the screen
Column {
Box(modifier = Modifier
.heightIn(min = 200.dp, max = screenWidth)
.aspectRatio(1f)
.border(BorderStroke(1.dp, Color.Green))
.align(Alignment.CenterHorizontally),
) {
Image(
painter = painterResource(id = R.drawable.puppy),
contentDescription = null,
contentScale = ContentScale.Crop
)
}
OptionsGrid(choicesList, modifier = Modifier.heightIn(max = screenHeight - 200.dp))
}
@Composable
fun OptionsGrid(choicesList: List<List<String>>, …Run Code Online (Sandbox Code Playgroud) 我尝试在他使用的地方使用这个答案OnGloballyPositionedModifier,但它没有返回我孩子的正确绝对 Pos。我尝试过positionInWindow(),positionInRoot()。当使用间距时它似乎有效,column但当我使用 时则无效offset。
@Composable
fun GetAbsolutePos(
content: @Composable () -> Unit,
) {
Box(modifier = Modifier
.onGloballyPositioned { coordinates ->
println("My coordinates: " + coordinates.positionInRoot())
}
) {
content()
}
}
@Preview
@Composable
fun test() {
GetAbsolutePos() {
Box(
modifier = Modifier
.width(PixelToDp(pixelSize = 200))
.offset(x = PixelToDp(pixelSize = 100), y = PixelToDp(pixelSize = 50))
.height(PixelToDp(pixelSize = 200))
.background(Color.Red)
) {
GetAbsolutePos() {
Column(
) {
GetAbsolutePos() { …Run Code Online (Sandbox Code Playgroud)