動畫

Prerequisites:

animateContentSize()

animateContentSize會根據大小不同產生動畫。
img

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Composable
fun AnimateContentSizeExample() {
  var isExpanded by remember { mutableStateOf(false) }
  Box(
    modifier = Modifier
      .fillMaxWidth()
      .animateContentSize()
      .background(Color.Blue)
      .padding(16.dp)
      .clickable {
        isExpanded = !isExpanded
      }
) {
    Column {
      Text("click")
      if(isExpanded) {
        Text("hello \n" +
            "world \n" +
            "hi \n",
          modifier = Modifier.background(Color.White))
      }
    }
  }
}

以下是官網的範例

https://developer.android.com/develop/ui/compose/animation/composables-modifiers#animatedvisibility

animateContentSize()要放置在size大小相關的函式之前。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var expanded by remember { mutableStateOf(false) }
Box(
    modifier = Modifier
        .background(colorBlue)
        .animateContentSize()
        .height(if (expanded) 400.dp else 200.dp)
        .fillMaxWidth()
        .clickable(
            interactionSource = remember { MutableInteractionSource() },
            indication = null
        ) {
            expanded = !expanded
        }

) {
}

results matching ""

    No results matching ""