Android学习记录(三十)-- Android 关于手势Gesture的简单实现和实践中遇到的问题。

1.初识gesture:

Android提供的手势:GestureDetector.OnGestureListener。

If you only want to listen for a subset it might be easier to extend GestureDetector.SimpleOnGestureListener.


基本的手势监听包括:

// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
public boolean onDown(MotionEvent arg0) {

/*
 * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发
 * 注意和onDown()的区别,强调的是没有松开或者拖动的状态
 */
public void onShowPress(MotionEvent e) {

// 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
public boolean onSingleTapUp(MotionEvent e) {

// 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

// 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

// 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
public void onLongPress(MotionEvent e) {

public boolean onDoubleTap(MotionEvent e) {
    //双击时产生一次

public boolean onDoubleTapEvent(MotionEvent e) {
    //双击时产生两次

触发手势监听的方法:

mGestureDetector = new GestureDetector(this);

在onTouch方法中,触发GestureDetector触发。

public boolean onTouchEvent(MotionEvent ev) {

      mGestureDetector.onTouchEvent(ev)

遇到的问题

1.持续滑动,怎么判断onScroll结束?

public boolean onTouchEvent(MotionEvent ev) {
        if (mGestureDetector.onTouchEvent(ev)) return true;
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            if (mIsScrolling) {
                mIsScrolling = false;
                handleScrollFinished();
            }
        }
        return false;
    }
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    if (!mIsScrolling) {
	mIsScrolling = true;
 

2.长按后再滑动无法触发onScroll

public void setIsLongpressEnabled (boolean isLongpressEnabled)
Added in  API level 1

Set whether longpress is enabled, if this is enabled when a user presses and holds down you get a longpress event and nothing further. If it's disabled the user can press and hold down and then later moved their finger and you will get scroll events. By default longpress is enabled.

设置为false后,长按后滑动后,可以看到onScroll,不过这样就不会触发长按事件,需要自己判断长按事件。



3.onFling需要注意的地方

onFling触发的时候,如果加打印的话,可以先看到触发onScroll事件,然后触发onFling事件,所以在onFling结束的时候,需要onScroll的暂态数据全部清空。


4.注意onScroll,onFling参数distance的具体含义

public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
Added in  API level 1

Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent. The calculated velocity is supplied along the x and y axis in pixels per second.

Parameters
e1 The first down motion event that started the fling.
e2 The move motion event that triggered the current onFling.
velocityX The velocity of this fling measured in pixels per second along the x axis.
velocityY The velocity of this fling measured in pixels per second along the y axis.
public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
Added in  API level 1

Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent. The distance in x and y is also supplied for convenience.

Parameters
e1 The first down motion event that started the scrolling.
e2 The move motion event that triggered the current onScroll.
distanceX The distance along the X axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.
distanceY The distance along the Y axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.

以上就是android gesture的简要说明。希望对大家有帮助。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Jetpack Compose ,可以将点击和拖动的功能分别封装成 Clickable 和 Draggable 两个 Composable 函数,并将它们组合起来使用,实现同时实现点击和拖动的效果。 Clickable Composable 函数用于监听点击事件,返回一个布尔值表示是否消耗了点击事件。 ```kotlin @Composable fun Clickable( onClick: () -> Unit, children: @Composable () -> Unit ) { val state = remember { mutableStateOf(false) } val gesture = remember { MutableInteractionSource() } val clickableModifier = Modifier.pointerInput(Unit) { detectTapGestures( onPress = { state.value = true }, onRelease = { if (state.value) onClick() }, onCancel = { state.value = false }, onTap = { state.value = false } ) gesture.tryAwaitRelease() } Box( modifier = clickableModifier, propagateMinConstraints = true, propagateMaxConstraints = true ) { children() } } ``` Draggable Composable 函数用于监听拖动事件,返回拖动后的位置。 ```kotlin @Composable fun Draggable( state: DragState, onDrag: (Offset) -> Unit, children: @Composable () -> Unit ) { val offsetX = state.position.x val offsetY = state.position.y val density = LocalDensity.current.density val draggableModifier = Modifier.pointerInput(Unit) { detectDragGestures( onDragStart = { state.isDragging = true }, onDragEnd = { state.isDragging = false }, onDrag = { change, dragAmount -> state.position += dragAmount / density onDrag(state.position) change.consumePositionChange() } ) } Box( modifier = draggableModifier.offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }, propagateMinConstraints = true, propagateMaxConstraints = true ) { children() } } class DragState(var isDragging: Boolean = false, var position: Offset = Offset.Zero) ``` 使用 Clickable 和 Draggable 组合实现同时点击和拖动的效果。 ```kotlin @Composable fun ClickableAndDraggable( onClick: () -> Unit, onDrag: (Offset) -> Unit, children: @Composable () -> Unit ) { val state = remember { DragState() } Draggable( state = state, onDrag = onDrag, children = { Clickable( onClick = onClick, children = children ) } ) } ``` 调用 ClickableAndDraggable 函数即可实现同时点击和拖动的效果。 ```kotlin var position by remember { mutableStateOf(Offset.Zero) } ClickableAndDraggable( onClick = { /* 处理点击事件 */ }, onDrag = { p -> position = p } ) { Box( Modifier .background(Color.Red) .size(50.dp) ) { Text("Drag me!", Modifier.align(Alignment.Center)) } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值