Compose的手势(六)
文章目录
- 一、前言
- 二、clickable
- 三、detectTapGestures
- 四、Dragging
- 五、Swiping
- 六、多点触控:平移、缩放、旋转
- 七、事件分发
- 八、基础事件
- 八、参考链接
一、前言
Compose
中手势会比在xml的方式要使用更广泛些,所以手势是有必要记录一下的。
二、clickable
在Compose
中对手势封装了一些单击、双击、长按事件。不过点击事件用的更多一些,这里进行简单的演示
@Composable
fun ClickableSample() {val count = remember { mutableStateOf(0) }// content that you want to make clickableText(text = count.value.toString(),modifier = Modifier.clickable { count.value += 1 })
}
三、detectTapGestures
如果想要对基本的手势进行监测,则可以使用PointerInputScope.detectTapGestures
或 PointerInputScope.detectDragGestures
PointerInputScope.detectTapGestures
的大致用法如下:
Modifier.pointerInput(Unit) {detectTapGestures(onPress = { /* Called when the gesture starts */ },onDoubleTap = { /* Called on Double Tap */ },onLongPress = { /* Called on Long Press */ },onTap = { /* Called on Tap */ })
}
具体示例如下
@Composable
private fun scrollBoxes() {Column(modifier = Modifier.background(Color.LightGray).size(100.dp).verticalScroll(rememberScrollState()).pointerInput(Unit){detectTapGestures(onPress = { /* Called when the gesture starts */ },onDoubleTap = { /* Called on Double Tap */ },onLongPress = { /* Called on Long Press */ },onTap = { /* Called on Tap */ })}) {repeat(10) {Text("Item $it", modifier = Modifier.padding(2.dp))}}
}
PointerInputScope.detectDragGestures
手势拖动教程可以参考以下链接:
https://developer.android.google.cn/reference/kotlin/androidx/compose/foundation/gestures/package-summary#(androidx.compose.ui.input.pointer.PointerInputScope).detectDragGestures(kotlin.Function1,kotlin.Function0,kotlin.Function0,kotlin.Function2)
四、Dragging
dragging可以检测手势单一方向拖动,但是并不会移动元素。如果想移动的话可以通过offset
来进行移动,如下所示:
var offsetX by remember { mutableStateOf(0f) }
Text(modifier = Modifier.offset { IntOffset(offsetX.roundToInt(), 0) }.draggable(orientation = Orientation.Horizontal,state = rememberDraggableState { delta ->offsetX += delta}),text = "Drag me!"
)
如果想要控制整个手势拖动,可以pointerInput
来进行监测,如下所示:
Box(modifier = Modifier.fillMaxSize()) {var offsetX by remember { mutableStateOf(0f) }var offsetY by remember { mutableStateOf(0f) }Box(Modifier.offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }.background(Color.Blue).size(50.dp).pointerInput(Unit) {detectDragGestures { change, dragAmount ->change.consumeAllChanges()offsetX += dragAmount.xoffsetY += dragAmount.y}})
}
五、Swiping
有时候会有滑动切换或者滑动关闭等类似需求,可以使用swipeable
修饰符进行实现,该 swipeable
修改器允许您将它发布的时候,通常对两个或两个以上的锚点动画中的方向定义的元素。对此的常见用法是实现“滑动到关闭”模式。
需要注意的是,这个修饰符不会移动元素,它只检测手势。您需要保持状态并在屏幕上表示它,例如,通过offset
修改器移动元素 。
swipeable
修改器中需要可滑动状态,可以使用 来创建和记住 rememberSwipeableState()
。这种状态下还提供了(见以编程动画的一组有用的方法来锚 snapTo
, animateTo
, performFling
,和 performDrag
),以及属性来观察拖动进展。
滑动手势可以配置为具有不同的阈值类型,例如 FixedThreshold(Dp)
和 FractionalThreshold(Float)
,并且对于每个锚点从到组合,它们可以不同。
为了获得更大的灵活性,您可以配置resistance
何时滑动越过边界,并且velocityThreshold
即使thresholds
尚未到达位置,也可以将滑动动画设置为下一个状态。
@ExperimentalMaterialApi@Composableprivate fun swipeableSample() {val width = 96.dpval squareSize = 48.dpval swipeableState = rememberSwipeableState(0)val sizePx = with(LocalDensity.current) { squareSize.toPx() }Log.e("YM","sizePx--$sizePx")val anchors = mapOf(0f to 0, sizePx to 1) // Maps anchor points (in px) to statesBox(modifier = Modifier.width(width).swipeable(state = swipeableState,anchors = anchors,//主要是存储一些滑动状态,可以在其它地方使用thresholds = { _, _ -> FractionalThreshold(0.3f) },//该值表示远离多少时候回弹到原先位置orientation = Orientation.Horizontal //水平).background(Color.LightGray)) {Box(Modifier.offset { IntOffset(swipeableState.offset.value.roundToInt(), 0) }.size(squareSize).background(Color.DarkGray))}}
第二个swipeable
,用来加深对anchors
的理解
@ExperimentalMaterialApi@Composableprivate fun swipeableSample2() {// Draw a slider-like composable consisting of a red square moving along a
// black background, with three states: "A" (min), "B" (middle), and "C" (max).val width = 350.dpval squareSize = 50.dpval swipeableState = rememberSwipeableState("A")val sizePx = with(LocalDensity.current) { (width - squareSize).toPx() }val anchors = mapOf(0f to "A", sizePx / 2 to "B", sizePx to "C")Box(modifier = Modifier.width(width).swipeable(state = swipeableState,anchors = anchors,thresholds = { _, _ -> FractionalThreshold(0.5f) },orientation = Orientation.Horizontal).background(Color.Black)) {Box(Modifier.offset { IntOffset(swipeableState.offset.value.roundToInt(), 0) }.size(squareSize).background(Color.Red),contentAlignment = Alignment.Center) {Text(swipeableState.currentValue, color = Color.White, fontSize = 24.sp)}}}
六、多点触控:平移、缩放、旋转
要检测用于平移、缩放和旋转的多点触控手势,您可以使用transformable
修饰符。此修改器本身不会变换元素,它只检测手势。
@Composable
fun TransformableSample() {// set up all transformation statesvar scale by remember { mutableStateOf(1f) }var rotation by remember { mutableStateOf(0f) }var offset by remember { mutableStateOf(Offset.Zero) }val state = rememberTransformableState { zoomChange, offsetChange, rotationChange ->scale *= zoomChangerotation += rotationChangeoffset += offsetChange}Box(Modifier// apply other transformations like rotation and zoom// on the pizza slice emoji.graphicsLayer(scaleX = scale,scaleY = scale,rotationZ = rotation,translationX = offset.x,translationY = offset.y)// add transformable to listen to multitouch transformation events// after offset.transformable(state = state).background(Color.Blue).fillMaxSize())
}
如果您需要将缩放、平移和旋转与其他手势结合使用,则可以使用 PointerInputScope.detectTransformGestures
检测器。
七、事件分发
https://compose.net.cn/design/gesture/custom_gesture/
八、基础事件
上述的事件都是从基础事件封装的,这里简单记录一下基础事件的使用链接,后续完善,可以实现按下时候一个效果,抬起时候一个效果,这种效果上述的几种手势无法完成该操作
- Compose实现状态选择器(原生drawable select标签效果) InteractionSource:https://blog.csdn.net/qq_33505109/article/details/124478228
- InteractionSource:https://developer.android.google.cn/reference/kotlin/androidx/compose/foundation/interaction/InteractionSource#public-properties_1
- 处理用户互动:https://developer.android.google.cn/jetpack/compose/handling-interaction#icon-button-example
八、参考链接
Modifier
https://developer.android.google.cn/reference/kotlin/androidx/compose/ui/Modifie
监测手势拖动detectDragGestures
https://developer.android.google.cn/reference/kotlin/androidx/compose/foundation/gestures/package-summary#(androidx.compose.ui.input.pointer.PointerInputScope).detectDragGestures(kotlin.Function1,kotlin.Function0,kotlin.Function0,kotlin.Function2)
androidx.compose.material
https://developer.android.google.cn/reference/kotlin/androidx/compose/material/package-summary
swipeable中的可选修饰符
https://developer.android.google.cn/reference/kotlin/androidx/compose/material/package-summary#swipeable(androidx.compose.ui.Modifier,androidx.compose.material.SwipeableState,kotlin.collections.Map,androidx.compose.foundation.gestures.Orientation,kotlin.Boolean,kotlin.Boolean,androidx.compose.foundation.interaction.MutableInteractionSource,kotlin.Function2,androidx.compose.material.ResistanceConfig,androidx.compose.ui.unit.Dp)
transformable的修饰符说明
https://developer.android.google.cn/reference/kotlin/androidx/compose/foundation/gestures/package-summary?hl=en#(androidx.compose.ui.Modifier).transformable(androidx.compose.foundation.gestures.TransformableState,kotlin.Boolean,kotlin.Boolean)
Compose的手势(六)相关推荐
- Jetpack Compose之手势使用
前言 在一个应用中用户的交互操作非常多,如:点击.滑动.拖动等,这些操作统统离不开手势. 在正式介绍Compose手势前,我们先来了解下Jetpack Compose手势整体框架,做到心中有图. 一. ...
- Jetpack Compose中的手势操作
点击事件 监听点击事件非常简单,使用 clickable 和 combinedClickable 修饰符即可满足需求: @OptIn(ExperimentalFoundationApi::class) ...
- Jetpack Compose入门详解(实时更新)
Jetpack Compose入门详解 前排提醒 前言(Compose是什么) 1.实战准备 一.优势与缺点 二.前四课 三.标准布局组件 1.Column 2.Row 3.Box 四.xml和com ...
- Compose 动画api之我的电子木鱼青春版
提示:需要对基本的compose语法有所了解` 文章目录 前言 一.总体规划 二.我的木鱼 1.敲击监听 pointerInput,detectTapGestures 2.木鱼动画 3.木鱼文字 三. ...
- 基于 Compose Canvas 的蛛网图组件开发
基于 Compose & Canvas 的蛛网图组件开发 1. 前言 2. 实现过程 2.1 准备工作 2.1.1创建Compose方法,确定参数 2.1.2 添加Canvas 2.1.3编写 ...
- 【Docker】Docker进阶(二)
Docker进阶 三.Docker发布微服务 四.Docker网络 4.1.Docker 网卡 4.2.docker网络命令 4.3.Docker 网络模式 4.3.1.bridge模式 4.3.2. ...
- Web前端学习笔记(3)
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 四十六.标签群组通配等选择器 四十七.层次选择器 47.1后代 M N 47.2父子M>N 47.3兄弟M~N 47. ...
- 使用Docker安装Guacamole远程网关并配置录像回放
一.参考 guacamole配置 guacamole使用Docker安装 guacamole在浏览器中播放录像 guacamole插件下载 二.环境 操作系统:Anolis OS 8.6 QU1 do ...
- Compose 手势事件:防止重复点击,双击,长按,全局触摸隐藏键盘
前言 JetPack Compose (后续简称compose) release版已经出来了三四个月了,虽然没正式版之前也学过几次,但一直没有机会用,在加上api的变更,导致之前学的都忘完了,现在终于 ...
- Jetpack-Compose 学习笔记(六)——Compose 主题 Theme 一探究竟,换肤还能如此 Easy Silky?
断更一时爽,一直断更一直爽~ 哈哈哈,就当给自己放了个长假吧.最近的行情太糟了,身边有同学已经被毕业,两个多月终于降薪找到下家··· 这里呼吁大家一定要存好六个月没有工作还能正常生活的银子,以备不时之 ...
最新文章
- 「AI白身境」究竟谁是paper之王,全球前10的计算机科学家
- JAVA实现链表面试题
- Mobile and MID opportunities in APAC
- java hssfcell 单元格样式_Java使用poi进行对Excel的操作
- python第六篇:Python复制超大文件、复制二进制文件
- 拒绝低效加班,别让“囚徒困境”限制你的职业发展
- 【Qt教程】1.3 - Qt5 工程文件的功能解读、快捷键
- 存储基础(VG、LV、LP、PV、PP)
- 二、saltstack基础配置
- 使用mybatis生成UUID
- udp push java ddpush_DDPush首页、文档和下载 - 任意门推送 - OSCHINA - 中文开源技术交流社区...
- 面试题-mysql优化
- 监控流媒体服务器的搭建和使用
- 隐马尔可夫模型(HMM)详解
- swift 指定区域截图,自定义不规则图形截图
- 【计算机图形学】【实验报告】太阳系绘制、B样条曲线绘制(附代码)
- 机器学习sklearn基础(1):多元逻辑回归分类器 (pcolormesh说明及绘图)
- 这么黄的教程,我看了2小时就关了!
- 利用XML制作UGUI登陆界面
- 深度学习:自编码器、深度信念网络和深度玻尔兹曼机