前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >安卓软件开发:Jetpack Compose中常见的核心概念总结-3

安卓软件开发:Jetpack Compose中常见的核心概念总结-3

原创
作者头像
Nimyears
修改2024-11-06 08:48:19
修改2024-11-06 08:48:19
710
举报

11. 绘制和动画

Canvas - 自定义绘制

代码语言:kotlin
复制
Canvas(modifier = Modifier.size(100.dp)) {
    drawCircle(
        color = Color.Blue,
        radius = size.minDimension / 2,
        center = center
    )
}
  • Canvas: 用于自定义绘制形状或图形。
  • drawCircle: 在Canvas上绘制一个圆形。

AnimatedContent - 内容动画

代码语言:kotlin
复制
AnimatedContent(targetState = count) { targetCount ->
    Text("Count: $targetCount")
}
  • AnimatedContent: 组件内容的变化时带有动画效果。

12. 滑动和手势

Scrollable Column - 可滑动列

代码语言:kotlin
复制
Column(
    modifier = Modifier
        .fillMaxSize()
        .verticalScroll(rememberScrollState())
) {
    repeat(50) {
        Text("Item $it", modifier = Modifier.padding(8.dp))
    }
}
  • verticalScroll: 为列添加垂直滚动功能。

GestureDetector - 手势检测

代码语言:kotlin
复制
Box(
    modifier = Modifier
        .size(200.dp)
        .background(Color.LightGray)
        .pointerInput(Unit) {
            detectTapGestures(
                onTap = { /* Handle tap */ },
                onDoubleTap = { /* Handle double tap */ }
            )
        }
)
  • detectTapGestures: 检测点击手势,包括单击、双击等。

13. 导航

Navigation - 导航

代码语言:kotlin
复制
NavHost(navController, startDestination = "home") {
    composable("home") { HomeScreen() }
    composable("details") { DetailScreen() }
}
  • NavHost: 定义导航主机,管理多个页面之间的导航。
  • composable: 定义每个页面对应的Composable。

Navigate to another screen - 导航到另一个屏幕

代码语言:kotlin
复制
Button(onClick = { navController.navigate("details") }) {
    Text("Go to Details")
}

14. LazyGrid 和自适应布局

LazyVerticalGrid - 垂直网格

代码语言:kotlin
复制
LazyVerticalGrid(
    cells = GridCells.Fixed(2),
    contentPadding = PaddingValues(8.dp)
) {
    items(20) { index ->
        Box(
            modifier = Modifier
                .padding(8.dp)
                .aspectRatio(1f)
                .background(Color.Gray)
        ) {
            Text("Item $index")
        }
    }
}
  • LazyVerticalGrid: 创建一个支持懒加载的网格布局。
  • GridCells.Fixed: 固定列数的网格。

LazyHorizontalGrid - 水平网格

代码语言:kotlin
复制
LazyHorizontalGrid(
    rows = GridCells.Fixed(2),
    modifier = Modifier.fillMaxSize()
) {
    items(30) { index ->
        Box(
            modifier = Modifier
                .padding(8.dp)
                .aspectRatio(1f)
                .background(Color.LightBlue)
        ) {
            Text("Item $index")
        }
    }
}
  • LazyHorizontalGrid: 创建一个支持懒加载的水平网格布局。

15. 带有状态的组件

Switch - 开关

代码语言:kotlin
复制
var isChecked by remember { mutableStateOf(false) }
Switch(
    checked = isChecked,
    onCheckedChange = { isChecked = it }
)
  • Switch: 创建一个带有开关功能的组件。

CheckBox - 复选框

代码语言:kotlin
复制
var isChecked by remember { mutableStateOf(false) }
Checkbox(
    checked = isChecked,
    onCheckedChange = { isChecked = it }
)
  • Checkbox: 创建一个复选框。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 11. 绘制和动画
    • Canvas - 自定义绘制
    • AnimatedContent - 内容动画
  • 12. 滑动和手势
    • Scrollable Column - 可滑动列
    • GestureDetector - 手势检测
  • 13. 导航
    • Navigation - 导航
    • Navigate to another screen - 导航到另一个屏幕
  • 14. LazyGrid 和自适应布局
    • LazyVerticalGrid - 垂直网格
    • LazyHorizontalGrid - 水平网格
  • 15. 带有状态的组件
    • Switch - 开关
    • CheckBox - 复选框
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档