当前位置:网站首页>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 = "悄悄的我走了,
正如我悄悄的来;
我挥一挥衣袖,
不带走一片云彩。"
)
边栏推荐
- Pandora IOT development board learning (HAL Library) - Experiment 1 running lantern (RGB) experiment (learning notes)
- Lvgl 8.2 picture scaling and rotation
- MySQL export SQL script file
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
- CSDN daily one practice 2021.11.06 question 1 (C language)
- Android 开发面试真题进阶版(附答案解析)
- MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
- Dell et Apple, deux entreprises de PC établies, se sont effondrées rapidement
- [rust weekly database] num bigint - large integer
- Qt之实现QQ天气预报窗体翻转效果
猜你喜欢

The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented

OceanBase 安装 yum 源配置错误及解决办法

【深度学习】深度学习检测小目标常用方法

ArrayList and sequence table

数学知识复习:第二型曲线积分

无心剑中译狄金森《灵魂择其伴侣》

腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题

科普达人丨漫画图解什么是eRDMA?

MATLAB image histogram equalization, namely spatial filtering

19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
随机推荐
Matplotlib notes: contour & Contour
吴恩达2022机器学习专项课测评来了!
再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
深潜Kotlin协程(十八):冷热数据流
文件共享服务器
智能DNA分子纳米机器人模型来了
Mysql database foundation: TCL transaction control language
matplotlib 笔记: contourf & contour
8行代码实现快速排序,简单易懂图解!
数据库什么时候需要使用索引【杭州多测师】【杭州多测师_王sir】
[rust daily] several new libraries were released on January 23, 2021
How can the sports app keep the end-to-side background alive to make the sports record more complete?
05_ Node JS file management module FS
WireGuard简单配置
SQL必需掌握的100个重要知识点:创建和操纵表
sublist3r报错解决
The reasoning delay on iphone12 is only 1.6 MS! Snap et al. Analyzed the transformer structure latency in detail, and used NAS to find out the efficient network structure of mobile devices
经典面试题:负责的模块,针对这些功能点你是怎么设计测试用例的?【杭州多测师】【杭州多测师_王sir】...
Review of mathematical knowledge: curve integral of the second type
iptables目标TPROXY