Android onTouch事件传递机制

SAP HR模块配置假期日历和缺勤类型 SAP的HR模块。业务顾问在实施时一般会配置未来10年的假期日历,到期后再进行配置。同时,增加请假类型(缺勤信息类型)。记录这一过程。 阅读详情

Android onTouch事件介绍:


Android的触摸事件:onClick, onScroll, onFling等等,都是由许多个Touch组成的。其中Touch的第一个状态肯定是ACTION_DOWN, 表示按下了屏幕。之后,touch将会有后续事件,可能是:

ACTION_MOVE  //表示为移动手势

ACTION_UP  //表示为离开屏幕

ACTION_CANCEL  //表示取消手势,不会由用户产生,而是由程序产生的

一个Action_DOWN, n个ACTION_MOVE, 1个ACTION_UP,就构成了Android中众多的事件。

在Android中,有一类控件是中还可以包含其他的子控件,这类控件是继承于ViewGroup类,例如:ListView, Gallery, GridView,LinearLayout。

还有一类控件是不能再包含子控件,例如:TextView。

在触发OnTouch事件的时候Android的GroupView会调用如下三个函数:

public boolean dispatchTouchEvent(MotionEvent ev)      //用于事件的分发

public boolean onInterceptTouchEvent(MotionEvent ev)    //  用于事件的拦截

public boolean onTouchEvent(MotionEvent ev)     //处理事件


本文的主要讨论对象就是ViewGroup类的控件嵌套时事件触发情况。

对于ViewGroup类的控件,有一个很重要的方法,就是onInterceptTouchEvent(),用于处理事件并改变事件的传递方向,它的返回值是一个布尔值,决定了Touch事件是否要向它包含的子View继续传递,这个方法是从父View向子View传递。而方法onTouchEvent(),用于接收事件并处理,它的返回值也是一个布尔值,决定了事件及后续事件是否继续向上传递,这个方法是从子View向父View传递。touch事件在 onInterceptTouchEvent()和onTouchEvent以及各个childView间的传递机制完全取决于onInterceptTouchEvent()和onTouchEvent()的返回值。返回值为true表示事件被正确接收和处理了,返回值为false表示事件没有被处理,将继续传递下去。

ACTION_DOWN事件会传到某个ViewGroup类的onInterceptTouchEvent,如果返回false,则DOWN事件继续向子ViewGroup类的onInterceptTouchEvent传递,如果子View不是ViewGroup类的控件,则传递给它的onTouchEvent。

如果onInterceptTouchEvent返回了true,则DOWN事件传递给它的onTouchEvent,不再继续传递,并且之后的后续事件也都传递给它的onTouchEvent。
如果某View的onTouchEvent返回了false,则DOWN事件继续向其父ViewGroup类的onTouchEvent传递;如果返回了true,则后续事件会直接传递给其onTouchEvent继续处理。(后续事件只会传递给对于必要事件ACTION_DOWN返回了true的onTouchEvent。

onInterceptTouchEvent()用于处理事件并改变事件的传递方向。处理事件这个不用说了,你在函数内部编写代码处理就可以了。而决定传递方向的是返回值,返回为false时事件会传递给子控件的onInterceptTouchEvent();返回值为true时事件会传递给当前控件的onTouchEvent(),而不在传递给子控件,这就是所谓的Intercept(截断)。

onTouchEvent() 用于处理事件,返回值决定当前控件是否消费(consume)了这个事件。可能你要问是否消费了又区别吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。

在没有重写onInterceptTouchEvent()和onTouchEvent()的情况下(他们的返回值都是false)。

 

onTouch事件传递测试:

<?xml version="1.0" encoding="utf-8"?>
<com.rpset.test.MyLinearLayout1 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <com.rpset.test.MyLinearLayout2
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical" >
        <com.rpset.test.MyTextView
            android:id="@+id/tv"
            android:layout_width="200px"
            android:layout_height="200px"
            android:background="#FFFFFF"
            android:text="MyTextView"
            android:textColor="#0000FF"
            android:textSize="40sp"
            android:textStyle="bold" />
    </com.rpset.test.MyLinearLayout2>
</com.rpset.test.MyLinearLayout1>


注: 当点击MyTextView时,程序会先进入到LinearLayout1的dispatchTouchEvent中,这个类必须调用super.dispatchTouchEvent(ev);否则后面的两个方法无法触发,所以发现这个方法根本没有必要重写,因为框架是在 super.dispatchTouchEvent(ev)中来调用onInterceptTouchEvent和onTouchEvent方法的,所以手动的设置dispatchTouchEvent的返回值是无效的,除非你不想让框架触发这两个方法。

 

对于MyTextView进行测试:

测试一:当三个view的dispatchTouchEvent,onInterceptTouchEvent(MyTextView没有此方法),onTouchEvent均返回false,也就是说事件始终没有被消费,那后续事件(ACTION_DOWN的ACTION_MOVE或者ACTION_UP)不会触发。Log信息如下:

04-09 12:01:55.019: D/MyLinearLayout(5435): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN
04-09 12:01:55.027: D/MyLinearLayout(5435): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN
04-09 12:01:55.043: D/MyLinearLayout(5435): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN
04-09 12:01:55.043: D/MyLinearLayout(5435): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN
04-09 12:01:55.047: D/MyLinearLayout(5435): MyTextView——dispatchTouchEvent action:ACTION_DOWN
04-09 12:01:55.051: D/MyLinearLayout(5435): MyTextView——-onTouchEvent action:ACTION_DOWN
04-09 12:01:55.051: D/MyLinearLayout(5435): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN
04-09 12:01:55.054: D/MyLinearLayout(5435): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN

结论:MyLinearLayout1,MyLinearLayout2,MyTextView都只处理了ACTION_DOWN,其余的TouchEvent被外层的Activity处理了

 

传递示意图:


 测试二:当只有MyTextView的onTouchEvent返回true,即事件最终在这里消费,(action:ACTION_MOVE会重复出现多次,这里仅代表一下) Log信息如下:

04-09 11:58:21.992: D/MyLinearLayout(4621): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN
04-09 11:58:21.992: D/MyLinearLayout(4621): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN
04-09 11:58:21.992: D/MyLinearLayout(4621): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN
04-09 11:58:22.000: D/MyLinearLayout(4621): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN
04-09 11:58:22.000: D/MyLinearLayout(4621): MyTextView——dispatchTouchEvent action:ACTION_DOWN
04-09 11:58:22.000: D/MyLinearLayout(4621): MyTextView——-onTouchEvent action:ACTION_DOWN

04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE
04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout1——onInterceptTouchEvent action:ACTION_MOVE
04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout2——dispatchTouchEvent action:ACTION_MOVE
04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout2——onInterceptTouchEvent action:ACTION_MOVE
04-09 11:58:22.133: D/MyLinearLayout(4621): MyTextView——dispatchTouchEvent action:ACTION_MOVE
04-09 11:58:22.133: D/MyLinearLayout(4621): MyTextView——-onTouchEvent action:ACTION_MOVE

04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP
04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout1——onInterceptTouchEvent action:ACTION_UP
04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout2——dispatchTouchEvent action:ACTION_UP
04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout2——onInterceptTouchEvent action:ACTION_UP
04-09 11:58:22.179: D/MyLinearLayout(4621): MyTextView——dispatchTouchEvent action:ACTION_UP
04-09 11:58:22.179: D/MyLinearLayout(4621): MyTextView——-onTouchEvent action:ACTION_UP

结论:MyTextView处理了所有的TouchEvent。

 

传递示意图:

对MyLinearLayout2进行测试:

测试一:当MyLinearLayout2的onInterceptTouchEvent方法返回true时,发送截断,事件不再向下传递而是直接给当前MyLinearLayout2的onTouchEvent处理,那后续事件(ACTION_DOWN的ACTION_MOVE或者ACTION_UP)不会触发。log信息如下:

04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN

结论:MyLinearLayout2,MyLinearLayout1都处理了ACTION_DOWN,其余的由最外层的Activity处理了。

 

传递示意图:


测试二:当MyLinearLayout2的onTouchEvent方法返回true时,后续的MOVE,Up事件会经过LayoutView1的onInterceptTouchEvent函数,然后到LayoutView2的onTouchEvent函数中去。不再经过MyLinearLayout2的onInterceptTouchEvent函数。 Log信息如下:

04-09 18:17:26.410: D/MyLinearLayout(21504): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN
04-09 18:17:26.410: D/MyLinearLayout(21504): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN
04-09 18:17:26.414: D/MyLinearLayout(21504): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN
04-09 18:17:26.414: D/MyLinearLayout(21504): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN
04-09 18:17:26.418: D/MyLinearLayout(21504): MyTextView——dispatchTouchEvent action:ACTION_DOWN
04-09 18:17:26.418: D/MyLinearLayout(21504): MyTextView——-onTouchEvent action:ACTION_DOWN
04-09 18:17:26.418: D/MyLinearLayout(21504): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN

04-09 18:17:26.437: D/MyLinearLayout(21504): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP
04-09 18:17:26.437: D/MyLinearLayout(21504): MyLinearLayout1——onInterceptTouchEvent action:ACTION_UP
04-09 18:17:26.441: D/MyLinearLayout(21504): MyLinearLayout2——dispatchTouchEvent action:ACTION_UP
04-09 18:17:26.445: D/MyLinearLayout(21504): MyLinearLayout2——-onTouchEvent action:ACTION_UP

结论:MyTextView只处理了ACTION_DOWN事件,MyLinearLayout2处理了所有的TouchEvent事件。


传递示意图:

 

测试三:当MyLinearLayout2的onInterceptTouchEvent和onTouchEvent 方法都返回true时,后续的MOVE,Up事件依然会经过LayoutView1的onInterceptTouchEvent函数,然后到LayoutView2的onTouchEvent函数中去。不再经过MyLinearLayout2的onInterceptTouchEvent函数。 Log信息如下:

04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN
04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN
04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN
04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN
04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN

04-09 13:17:30.547: D/MyLinearLayout(9935): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE
04-09 13:17:30.547: D/MyLinearLayout(9935): MyLinearLayout1——onInterceptTouchEvent action:ACTION_MOVE
04-09 13:17:30.547: D/MyLinearLayout(9935): MyLinearLayout2——dispatchTouchEvent action:ACTION_MOVE
04-09 13:17:30.551: D/MyLinearLayout(9935): MyLinearLayout2——-onTouchEvent action:ACTION_MOVE

04-09 13:17:30.644: D/MyLinearLayout(9935): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP
04-09 13:17:30.644: D/MyLinearLayout(9935): MyLinearLayout1——onInterceptTouchEvent action:ACTION_UP
04-09 13:17:30.644: D/MyLinearLayout(9935): MyLinearLayout2——dispatchTouchEvent action:ACTION_UP
04-09 13:17:30.648: D/MyLinearLayout(9935): MyLinearLayout2——-onTouchEvent action:ACTION_UP

结论:MyLinearLayout2处理了所有的TouchEvent。

 传递示意图:

对于MyLinearLayout1进行测试:

测试一:当MyLinearLayout1的onInterceptTouchEvent方法返回true时,发送截断,事件不再向下传递而是直接给当前MyLinearLayout1的onTouchEvent处理,那后续事件(ACTION_DOWN的ACTION_MOVE或者ACTION_UP)不会触发。log信息如下:

04-09 13:52:06.789: D/MyLinearLayout(12363): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN
04-09 13:52:06.789: D/MyLinearLayout(12363): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN
04-09 13:52:06.789: D/MyLinearLayout(12363): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN

结论:MyLinearLayout1只处理了ACTION_DOWN,其余的被外层的Avtivity处理了。

传递示意图:


测试二:当MyLinearLayout1的onTouchEvent 方法返回true时,后续的MOVE,Up事件会直接传给MyLinearLayout1的onTouchEvent()。不再经过MyLinearLayout1的onInterceptTouchEvent函数。Log信息如下:

04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN
04-09 18:26:58.125: D/MyLinearLayout(22294): MyTextView——dispatchTouchEvent action:ACTION_DOWN
04-09 18:26:58.125: D/MyLinearLayout(22294): MyTextView——-onTouchEvent action:ACTION_DOWN
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN

04-09 18:26:58.215: D/MyLinearLayout(22294): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE
04-09 18:26:58.215: D/MyLinearLayout(22294): MyLinearLayout1——-onTouchEvent action:ACTION_MOVE

04-09 18:26:58.281: D/MyLinearLayout(22294): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP
04-09 18:26:58.285: D/MyLinearLayout(22294): MyLinearLayout1——-onTouchEvent action:ACTION_UP

结论:MyTextView和MyLinearLayout2只处理了ACTION_DOWN事件,MyLinearLayout1处理了所有的TouchEvent。

传递示意图:



测试三:当MyLinearLayout1的onInterceptTouchEvent和onTouchEvent 方法都返回true时,后续的MOVE,Up事件会直接传给MyLinearLayout1的onTouchEvent(),不传给其他任何控件的任何函数。不再经过MyLinearLayout1的onInterceptTouchEvent函数。Log信息如下:

04-09 13:58:04.199: D/MyLinearLayout(13116): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN
04-09 13:58:04.199: D/MyLinearLayout(13116): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN
04-09 13:58:04.203: D/MyLinearLayout(13116): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN

04-09 13:58:04.324: D/MyLinearLayout(13116): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE
04-09 13:58:04.328: D/MyLinearLayout(13116): MyLinearLayout1——-onTouchEvent action:ACTION_MOVE

04-09 13:58:04.367: D/MyLinearLayout(13116): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP
04-09 13:58:04.367: D/MyLinearLayout(13116): MyLinearLayout1——-onTouchEvent action:ACTION_UP

结论:MyLinearLayout1处理了所有的TouchEvent。


 传递示意图:

 

 

参考:http://www.cnblogs.com/rocky_yi/archive/2011/01/21/1941522.html#

参考:http://hi.baidu.com/alittleagain/item/3b3e61d70a860608d78ed0ca


 

晶晨S905L3S/S905L3SB芯片刷机实战:BL加载工具与线刷固件全解析 本文针对晶晨S905L3S/S905L3SB芯片电视盒子刷机时常见的2%失败问题,提供了两种核心解决方案。一是使用已集成特殊通信协议的专用版USB Burning Tool(如V2.2.5/V2.2.7),二是采用旧版工具配合独立的BootLoader加载工具。文章详细解析了两种方案的原理、操作步骤、固件选择及常见问题排查,旨在帮助用户成功完成刷机,让老旧设备焕发新生。 阅读详情

相关推荐

Ubuntu22.04中通过conda快速部署PyTorch开发环境(CPU/GPU双版本实战)

本文详细介绍了在Ubuntu22.04系统中,如何利用Conda高效部署PyTorch开发环境。文章分别提供了CPU与GPU双版本的实战教程,涵盖环境创建、镜像加速、版本匹配及深度验证等关键步骤,旨在帮助开发者快速搭建稳定、兼容的深度学习环境,从而专注于模型开发。

rose2的博客 557

Android View事件分发机制 onTouch和onTouchEvent区别 以及解决滑动冲突的思路

参考: https://www.jianshu.com/p/7d50a6b0b6af 总结一下: 子View中的监听事件优先级: OnTouchListener---->OnTouchEvent---->OnCLickListener 如果优先级高的拦下事件,就不会再传递给后面的监听事件 1.Touch事件分发中只有两个主角:ViewGroup和View。View...

emmmsuperdan的博客 4246

从AMBA到USB:手把手教你评估和选择Synopsys DesignWare里的免费商用IP

本文详细介绍了如何评估和选择Synopsys DesignWare中的免费商用IP,从AMBA到USB等关键模块。通过技术参数核对、验证环境评估和集成注意事项,帮助工程师高效利用这些经过硅验证的商业级IP,提升SoC设计效率。文章还探讨了免费IP与付费IP的边界策略,避免常见陷阱。

weixin_42683394的博客 297

Android 关于View事件分发(onTouch、onTouchEvent、onClick、onLongClick的关系及原理)(一)

事件分发、事件冲突

weixin_54087854的博客 2925

NVIDIA DGX Spark (Ubuntu 24.04) 部署Isaac Sim与Isaac Lab:二进制安装与ROS2集成实战

本文详细介绍了在NVIDIA DGX Spark工作站(Ubuntu 24.04系统)上,通过预编译二进制包稳定部署Isaac Sim与Isaac Lab的完整实战流程。文章重点剖析了二进制安装相较于源码编译和pip安装的优势,有效规避了CUDA、PyTorch等关键组件的版本冲突陷阱,并逐步指导完成环境配置、Isaac Lab集成、Conda环境管理以及ROS2 Jazzy的集成,最终构建出完整的机器人仿真与强化学习开发平台。

kite3的博客 396

Python 静态导入与动态导入

python中模块的静态导入 与 动态导入(运行时指定模块),以及具体对应示例。

慢也好,步子小也好,往前走就好~ 717

Android事件传递机制(一)流程梳理

事件传递或者称之为事件分发是Android中非常重要和核心的知识点,我们在手机上的任何一次点击或者触摸都会涉及到事件传递,所以非常有必要好好了解一下相关知识。 当一个Touch事件发生后,首先是当前的Activity拿到该事件,然后Activity会将事件传递给Window,然后Window再将事件传递给当前界面的根布局(顶级父View),即按照Activity -> Window -&g...

beita08的博客 1521

2021年软件测试面试题大全

简述测试流程: 1、阅读相关技术文档(如产品PRD、UI设计、产品流程图等)。 2、参加需求评审会议。 3、根据最终确定的需求文档编写测试计划。 4、编写测试用例(等价类划分法、边界值分析法等)。 5、用例评审(主要参与人员:开发、测试、产品、测试leader)。 6、开发提交代码至SVN或者GIT ,配管搭建测试环境。 7、执行测试用例,记录发现的问题。 8、验证bug与回归测试。 9、编写测试报告。 10、产品上线。 补充测试用例设计过程: 根据需求得出测试需求 设计测试方

hard_days的博客 20万+

Android Touch事件分发(源码分析)

源码分析:Activity事件分发机制;ViewGroup事件分发机制;View事件分发机制。

帅次的博客 1658

超详细YOLOv8目标检测全程概述:环境、训练、验证与预测详解

这篇博文提供了一个关于使用YOLOv8进行目标检测的全面指南,包括环境搭建、模型训练、验证和预测的详细步骤,以及如何解释训练过程中生成的各种图表和数据。整篇文章为读者提供了一个关于如何利用YOLOv8进行目标检测的实践指南,从环境搭建到模型部署,再到结果分析,每一部分都给出了详细的步骤和解释。此外,还有对于模型输出的解释,帮助读者更好地理解和使用YOLOv8模型。

CDBmax的博客 10万+

ESP32开发环境避坑指南:用Thonny烧录MicroPython固件时常见的5个错误及解决方法

本文针对ESP32开发新手,详细解析了使用Thonny IDE烧录MicroPython固件时最常见的5个错误及其解决方案。内容涵盖驱动安装、端口识别、固件烧录失败、版本兼容性、连接不稳定以及文件系统操作异常等核心痛点,并提供硬件层面的深度排查指南,帮助开发者高效搭建稳定的ESP32开发环境。

weixin_29189003的博客 407

Android onTouch 方法 和onTouchEvent 方法

onTouch 是将触摸屏运动事件传递到目标视图,或查看是否是目标。‘ onTouch方法是view设置触摸时间的方法,结合OnTouchListener 使用, 下面是常见的2中按压的方法, public class MainActivity extends AppCompatActivity implements View.OnTouchListener { private LinearLayout linearLayout; private Button button;

小牧在一直在学习,在前进的道路上大家一起学习,进步。 3905

QT应用编程: 编写低功耗BLE蓝牙调试助手(Android系统APP)

一、环境介绍 QT版本: 5.12.6 编译环境: win10 64位 目标系统: Android 完整工程源码下载地址(包含APK文件):https://download.youkuaiyun.com/download/xiaolong1126626497/19051696 二、功能介绍 本软件是一款BLE蓝牙串口调试助手,支持常规的发送和接收调试,BLE是低功耗类型蓝牙,在智能家居、物联网领域使用较多。BLE低功耗蓝牙一般不能直接使用手机进行连接,手机蓝牙默认都是工作在传统蓝牙模式,无法直接连接BL..

7660

android onclick ontouch 事件传递机制

首先,这篇文章仅仅针对于由于触摸(Touch)而触发的事件。 Android的事件:onClick, onScroll, onFling等等,都是由许多个Touch组成的。其中Touch的第一个状态肯定是ACTION_DOWN, 表示按下了屏幕。之后,touch将会有后续事件,可能是: ACTION_MOVE //表示为移动手势 ACTION_UP //表示为离开屏幕 A

zhaoweixing1989的专栏 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Android事件传递机制之【onTouch方法】

PS:当时一不小心把返回值改为了true,让我纠结很久啊!经过教训所以记忆深刻 关于android事件传递的文章很多,我也不多谈,这里是为了记下我的教训 1.onTouch()方法会执行两次down事件和up事件 2.onTouch()的down事件返回true,即终止本次 3.onTouch()的down事件返回flase,则根据down事件和up事件的操作时间判断是点击

leolaurel的专栏 3238

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Android中事件传递机制的总结(onTouch、onClick、onLongClick等)

事件传递虽然算不上某个单独的知识点,但是在实际项目开发中肯定会碰到,如果不明白其中的原理,那在设计各种滑动效果时就会感到很困惑。 关于事件的传递,我们可能会有以下疑问: 事件是如何传递的 事件是如何处理的 自定义view的时候,事件也冲突了怎么解决 带着这三个疑问,我们来总结一下事件传递机制是怎么回事。   一、事件分发的原理: 1、事件是如何传递的:

思念叨火车的专栏 3677

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

android imageview 事件传递,Android 事件传递机制TextView,ImageView等没有默认clickable属性的View单独设置onTouch事件注意事项...

本文讲解TextView,ImageView等没有默认clickable属性的View单独设置onTouch事件Android 事件传递机制:Android 事件传递机制初涉我们知道 Button,TextView 等控件的基类都是View。只要你触摸到了任何一个控件,就一定会调用该控件的dispatchTouchEvent方法。那当我们去触摸TextView的时候,底层就会去调用TextView...

weixin_34268604的博客 429
上一篇: Android 后台线程弹对话框导致程序崩溃(is not valid; is your activity running)
下一篇: ViewPager嵌套ViewPager不能响应滑动事件解决方法
浅秋
博客等级 码龄16年 38粉丝 5原创
评论 33
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值