简体   繁体   English

自定义视图触摸事件

[英]Custom View Touch Events

I'm trying to implement a custom view in which the user can draw with his finger. 我正在尝试实现一个自定义视图,用户可以在其中用手指绘画。 Basically, I'm following this tutorial . 基本上,我正在学习本教程

Everything seems to be working nice, but, given the fact that I also have 2 buttons underneath the custom view like so: 一切似乎都运行良好,但是,考虑到我在自定义视图下还有两个按钮的事实,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color"
    tools:context=".MainActivity" 
    android:id="@+id/mainScreen" >

    <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="@string/save" />

    <Button
        android:id="@+id/buttonQuery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="@string/query" />

    <com.example.myapp.DrawView
        android:id="@+id/drawView"
        android:background="@color/red"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_above="@id/buttonSave" />

</RelativeLayout>

it actually draws even underneath the buttons and I'm having a hard time understanding why it doesn't capture only the touch events that occur on the DrawView and not outside it. 它实际上甚至在按钮下方绘制,而且我很难理解为什么它不只捕获发生在DrawView上而不发生在DrawView之外的触摸事件。 Basically, I can't start the line from above the buttons, but once I start drawing on my view, I can even extend the line outside of it. 基本上,我不能从按钮上方开始线条,但是一旦我开始在视图上绘图,我什至可以将线条延伸到其外部。

As far as I can tell, the DrawView doesn't overflow beneath the buttons, as discussed here so I want to figure out what's going on. 据我所知,正如下面所讨论的 ,DrawView不会在按钮下方溢出,所以我想知道发生了什么。 Do I really have to check if the events are out of bounds? 我真的需要检查事件是否超出范围吗? Does it actually delegate all of the touch events to the currently focused view? 它实际上是否将所有触摸事件都委派给当前聚焦的视图? Is there any way to change this behavior? 有什么办法可以改变这种行为?

This simple tutorial doesn't have the described issue, but it draws point by point. 这个简单的教程没有所描述的问题,但是它逐点绘制。


Update: Debugging the application, I determined that drawView has size 480x728, while mainScreen has 480x800, so they do not overlap. 更新:调试应用程序后,我确定drawView的大小为480x728,而mainScreen的大小为480x800,因此它们不会重叠。 It continues to register touch events even if they are triggered outside of the drawView, once it has focus. 一旦获得焦点,即使在drawView之外触发触摸事件,它也会继续注册。 This is what I'm trying to avoid and it would be nice to do it without passing the event coordinates through a big if statement... 这就是我要避免的事情,并且最好不要通过大的if语句传递事件坐标来做到这一点……

If you don't want the buttons to overlap the DrawView then use a LinearLayout to keep all of the View s separate. 如果您不希望按钮与DrawView重叠, DrawView使用LinearLayout将所有View分开。 At the moment, judging from the XML layout (and assuming there isn't anything funky going on with the layout programmatically in code), your buttons are overlapping the DrawView . 目前,从XML布局判断(并假设没有什么时髦的布局编程代码事),你的按钮重叠DrawView

I'm under the impression you're after having the two buttons placed at the top side by side, with the DrawView filling the remaining screen area underneath. 我给您的印象是两个按钮并排放置在顶部,而DrawView填充了下面的其余屏幕区域。 To do this, use an outer container LinearLayout with vertical orientation. 为此,请使用垂直方向的外部容器LinearLayout The first child of it is another LinearLayout with horizontal orientation, containing the two buttons. 它的第一个孩子是另一个水平放置的LinearLayout ,其中包含两个按钮。 The second child of the inner LinearLayout is the DrawView . 内部LinearLayout的第二个子级是DrawView Use width, height, and weight attributes to get desired dimensions. 使用宽度,高度和重量属性获得所需的尺寸。 (Or, another way is to keep the XML layout structure you have, but set some top margin on the DrawView to equal the buttons' height, which could be fixed in the XML layout, or done programmatically.) (或者,另一种方法是保留您拥有的XML布局结构,但是在DrawView上设置一些顶部边距以等于按钮的高度,可以将其固定在XML布局中,或者以编程方式完成。)

EDIT Sorry, I totally missed the layout_above attribute, which invalidates my second sentence. 编辑抱歉,我完全错过了layout_above属性,该属性使我的第二句话无效。 In that case, without being able to fire up Eclipse at the moment I'm not sure. 在那种情况下,目前还不能启动Eclipse。 Try the LinearLayout approach as suggested anyway. 无论如何,请尝试使用LinearLayout方法。

This turned out to be some weird bug in the way the Android emulator displays the interface. 原来这是Android模拟器显示界面方式中的一个怪异错误。 It may do something similar on real devices (throwing touch events even outside of the listener view), but since it limits the display to the drawView , I can safely ignore them for my application, since I just need to extract the drawn shape from the drawView . 它可能会在实际设备上做类似的事情(甚至在侦听器视图之外drawView触摸事件),但是由于它将显示限制为drawView ,因此对于我的应用程序,我可以放心地忽略它们,因为我只需drawView屏幕上提取绘制的形状即可。 drawView It may cause big headaches in other applications, though... 不过,这可能会在其他应用程序中引起头痛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM