【Android】关于touch设备TOOL_TYPE_STYLUS

这里通过log来查看触控笔的motionEvent,

java 复制代码
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(tag, "event=" + event);
        return true;
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        Log.i(tag, "G event=" + event);
        return true;
    }

在靠近屏幕时,调用到onGenericMotionEvent

在触碰到屏幕时,调用onTouchEvent,

toolType[0]=TOOL_TYPE_STYLUS

一次点击的完整log

java 复制代码
10-14 12:25:30.040  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_ENTER, actionButton=0, id[0]=0, x[0]=2142.9934, y[0]=1068.1862, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=142798665, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=16461402 }
10-14 12:25:30.067  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2146.1409, y[0]=1059.1779, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=13, eventTime=142798692, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=983447964 }
10-14 12:25:30.083  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2148.7458, y[0]=1056.4645, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=7, eventTime=142798710, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=645840300 }
10-14 12:25:30.092  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2149.6143, y[0]=1055.9219, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=142798713, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=62681315 }
10-14 12:25:30.099  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2152.1106, y[0]=1054.4023, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=5, eventTime=142798726, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=1026885681 }
10-14 12:25:30.108  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2155.041, y[0]=1053.5342, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798734, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=738838838 }
10-14 12:25:30.116  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2157.9717, y[0]=1052.9915, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798742, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=945623392 }
10-14 12:25:30.125  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2160.034, y[0]=1052.9915, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798750, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=209887185 }
10-14 12:25:30.133  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2163.1816, y[0]=1053.4255, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798759, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=372229236 }
10-14 12:25:30.141  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2165.678, y[0]=1054.0768, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798767, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=216054123 }
10-14 12:25:30.150  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2168.3914, y[0]=1054.728, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798775, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=101492618 }
10-14 12:25:30.153  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2171.756, y[0]=1054.9451, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=142798780, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=861304425 }
10-14 12:25:30.153  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_EXIT, actionButton=0, id[0]=0, x[0]=2171.756, y[0]=1054.9451, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=142798782, downTime=0, deviceId=7, source=0x5002, displayId=0, eventId=866096615 }
10-14 12:25:30.155  6190  6190 I test9   : event=MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=2171.756, y[0]=1054.9451, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=142798782, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=406230247 }
10-14 12:25:30.158  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2171.539, y[0]=1054.9451, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=142798785, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=120979047 }
10-14 12:25:30.166  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2170.3452, y[0]=1054.728, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=4, eventTime=142798794, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=72378347 }
10-14 12:25:30.174  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2169.6938, y[0]=1053.9683, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=4, eventTime=142798802, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=72421242 }
10-14 12:25:30.183  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2168.8257, y[0]=1053.2085, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=4, eventTime=142798810, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=507279949 }
10-14 12:25:30.191  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2168.3914, y[0]=1052.7744, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=4, eventTime=142798819, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=103703126 }
10-14 12:25:30.200  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2167.5232, y[0]=1052.6659, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=4, eventTime=142798827, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=385426269 }
10-14 12:25:30.208  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2167.0889, y[0]=1052.2317, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=4, eventTime=142798835, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=752640004 }
10-14 12:25:30.216  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2166.4377, y[0]=1052.1232, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=4, eventTime=142798844, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=42588775 }
10-14 12:25:30.225  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2166.2207, y[0]=1052.1232, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798852, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=1020026684 }
10-14 12:25:30.226  6190  6190 I test9   : event=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=2166.0037, y[0]=1052.1232, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x2000000, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=142798852, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=191886775 }
10-14 12:25:30.232  6190  6190 I test9   : event=MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=2166.0037, y[0]=1052.1232, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=142798854, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=1059956846 }
10-14 12:25:30.234  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_ENTER, actionButton=0, id[0]=0, x[0]=2165.7866, y[0]=1052.3402, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=142798854, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=156568735 }
10-14 12:25:30.235  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2165.3523, y[0]=1052.5574, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=142798858, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=277665343 }
10-14 12:25:30.241  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2164.2668, y[0]=1054.0768, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798867, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=98280364 }
10-14 12:25:30.249  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2161.4448, y[0]=1056.4645, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798875, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=421868677 }
10-14 12:25:30.257  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2157.9717, y[0]=1059.0693, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798883, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=485500730 }
10-14 12:25:30.266  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2154.4985, y[0]=1061.3485, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798891, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=152502290 }
10-14 12:25:30.274  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2147.8774, y[0]=1063.5192, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798900, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=926486184 }
10-14 12:25:30.283  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2139.086, y[0]=1066.0155, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=142798908, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=338019200 }
10-14 12:25:30.291  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2128.7747, y[0]=1070.3568, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=4, eventTime=142798918, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=1058054799 }
10-14 12:25:30.300  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_MOVE, actionButton=0, id[0]=0, x[0]=2126.9294, y[0]=1071.9849, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=142798924, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=497747189 }
10-14 12:25:30.300  6190  6190 I test9   : G event=MotionEvent { action=ACTION_HOVER_EXIT, actionButton=0, id[0]=0, x[0]=2126.9294, y[0]=1071.9849, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=142798928, downTime=142798782, deviceId=7, source=0x5002, displayId=0, eventId=588130330 }

在 Android 中,触控笔(Stylus)是一种特殊的输入设备,它可以提供更精确和细致的输入方式。Android 提供了相应的触控笔事件处理机制,以支持触控笔的使用。

触控笔事件与触摸事件略有不同,因为触控笔通常提供了更多的信息,如压力、倾斜角度等。在处理触控笔事件时,你可以使用以下方法和属性:

  1. onTouchEvent(): 在 ViewActivity 中重写 onTouchEvent() 方法,可以接收触控笔事件。你可以根据事件的类型和属性来处理触控笔事件。

  2. MotionEvent: 触控笔事件使用 MotionEvent 类表示,它提供了许多有用的方法来获取触控笔事件的信息。例如,使用 event.getToolType() 方法可以检测事件的工具类型,通过 event.getPressure() 方法可以获取触压力度,使用 event.getOrientation() 方法可以获取触控笔的倾斜角度等。

在 Android 中,onGenericMotionEvent() 是一个回调方法,用于处理通用输入事件(Generic Motion Event)。它是 View 类的一个方法,用于接收并处理与输入设备相关的通用事件。

通用输入事件可以包括鼠标、轨迹球、游戏手柄等多种输入设备的事件。这些事件与触摸事件略有不同,因为它们提供了更丰富的输入信息,如鼠标滚轮的滚动、轨迹球的移动、游戏手柄的摇杆操作等。

相关推荐
小蜜蜂嗡嗡34 分钟前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
aqi0040 分钟前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体
zhangphil2 小时前
Android理解onTrimMemory中ComponentCallbacks2的内存警戒水位线值
android
你过来啊你2 小时前
Android View的绘制原理详解
android
移动开发者1号5 小时前
使用 Android App Bundle 极致压缩应用体积
android·kotlin
移动开发者1号5 小时前
构建高可用线上性能监控体系:从原理到实战
android·kotlin
ii_best10 小时前
按键精灵支持安卓14、15系统,兼容64位环境开发辅助工具
android
美狐美颜sdk10 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭15 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
aqi0016 小时前
FFmpeg开发笔记(七十七)Android的开源音视频剪辑框架RxFFmpeg
android·ffmpeg·音视频·流媒体