当前位置:网站首页>Jetpack Compose DropdownMenu跟随手指点击位置显示
Jetpack Compose DropdownMenu跟随手指点击位置显示
2022-06-30 10:34:00 【JIULANG9】
DropdownMenu显示时默认会避开点击的view
通常默认显示在左下方
本篇文章教你实现跟随手指按下位置显示
效果图

实现方法
首先要获取到点击的位置之后计算偏移量
先分析两种offset参数
1使用DropdownMenu的offset参数
获取到点击的位置之后计算偏移量,
DropdownMenu的offset参数
@Composable
fun DropdownMenu(
...
offset: DpOffset = DpOffset(0.dp, 0.dp)
...
)
这种方法比较麻烦,要反向计算偏移量,因为初始位置(x,y)轴的原点在左下角,而不是左上角
2Modifier.offset
将DropdownMenu嵌套在Box里面,调用BoxD的Modifier.offset()改变DropdownMenu的显示位置
这种方案的的初始位置(x,y)轴的原点在左上角
点击的位置就是DropdownMenu的偏移量,
获取到点击的位置
层级结构
Box{
card()
Box{
DropdownMenu()
}
}
在最外层的Box创建一个用于监听点击事件的修饰符,来捕获点击位置
val animatedOffset = remember {
Animatable(Offset(0f, 0f), Offset.VectorConverter) }
Box(
modifier = Modifier
.fillMaxWidth()
.pointerInput(Unit) {
coroutineScope {
while (true) {
//获取点击位置
val boxOffset = awaitPointerEventScope {
awaitFirstDown().position
}
//显示DropdownMenu
expanded = true
launch {
animatedOffset.animateTo(
boxOffset,
animationSpec = spring(stiffness = Spring.StiffnessLow)
)
}
}
}
}
)
最后在DropdownMenu外层的Box()设置偏移量
Box(modifier = Modifier.offset {
IntOffset(
animatedOffset.value.x.roundToInt(),
animatedOffset.value.y.roundToInt()
)
} ) {
DropdownMenu()
}
完整代码
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun FullContent(
modifier: Modifier,
context: String
) {
var expanded by remember {
mutableStateOf(false) }
val animatedOffset = remember {
Animatable(Offset(0f, 0f), Offset.VectorConverter) }
Box(
modifier = Modifier
.fillMaxWidth()
.pointerInput(Unit) {
coroutineScope {
while (true) {
//获取点击位置
val boxOffset = awaitPointerEventScope {
awaitFirstDown().position
}
expanded = true
launch {
animatedOffset.animateTo(
boxOffset,
animationSpec = spring(stiffness = Spring.StiffnessLow)
)
}
}
}
}
) {
Card(modifier = modifier
.fillMaxWidth(),
border = BorderStroke(2.dp, CustomTheme.colors.divider)
) {
Text(
text = context,
modifier = Modifier.padding(6.dp),
fontSize = 17.sp,
fontWeight = FontWeight.Normal,
)
}
Box(modifier = Modifier.offset {
IntOffset(
animatedOffset.value.x.roundToInt(),
animatedOffset.value.y.roundToInt()
)
} ) {
DropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false }
) {
DropdownMenuItem(
text = {
Text(stringResource(id = R.string.app_copy))
},
onClick = {
})
DropdownMenuItem(
text = {
Text(stringResource(id = R.string.app_copy))
},
onClick = {
})
Divider()
DropdownMenuItem(
text = {
Text(stringResource(id = R.string.app_copy))
},
onClick = {
})
}
}
}
}
使用方法
FullContent(
modifier = Modifier.fillMaxWidth(),
context = "悄悄的我走了,
正如我悄悄的来;
我挥一挥衣袖,
不带走一片云彩。"
)
边栏推荐
- Mysql database foundation: TCL transaction control language
- LVGL 8.2 Image styling and offset
- LVGL 8.2 Drop down in four directions
- 无心剑中译狄金森《灵魂择其伴侣》
- 中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
- 历史上的今天:微软收购 PowerPoint 开发商;SGI 和 MIPS 合并
- 苹果高管公然“开怼”:三星抄袭 iPhone,只加了个大屏
- 8行代码实现快速排序,简单易懂图解!
- 经典面试题:负责的模块,针对这些功能点你是怎么设计测试用例的?【杭州多测师】【杭州多测师_王sir】...
- 透過華為軍團看科技之變(五):智慧園區
猜你喜欢
随机推荐
Key library function based on Hal Library
Lvgl 8.2 picture scaling and rotation
Classic interview question: responsible modules, how do you design test cases for these function points? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]
JS FAQs
OLAP数据库引擎如何选型?
7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
苹果高管公然“开怼”:三星抄袭 iPhone,只加了个大屏
SQL必需掌握的100个重要知识点:使用存储过程
20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...
MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
煥發青春的戴爾和蘋果夾擊,兩大老牌PC企業極速衰敗
滴滴开源敏捷测试用例管理平台!
Circuit breaker hystrixcircuitbreaker
文件共享服务器
LVGL 8.2 Simple Image button
Agile Development: super easy to use bucket estimation system
第一届中国数字藏品大会即将召开
[rust weekly database] num bigint - large integer
IDEA 又出新神器,一套代码适应多端!
[STL source code analysis] iterator









