【问题标题】:Android - VideoView - onTouch does not behave as expectedAndroid - VideoView - onTouch 的行为不符合预期
【发布时间】:2012-06-06 19:44:22
【问题描述】:

我正在学习教程并尝试通过 Android 的 VideoView 构建自定义视频播放器。
现在,当我触摸 VideoView 时,我想显示或隐藏媒体控制器。如果控制器已经显示,则隐藏它们,如果它们被隐藏,则显示它们。

这些媒体控制器由 2 个位于 VideoView 上方的 LinearLayout 表示。 以红色突出显示的区域代表背景中的 VideoView 边框。

我的问题是,当我触摸与 VideoView 重叠的媒体控制器区域时,媒体控制器会被隐藏。这不是想要的效果,因为我不是直接接触 VideoView,而是 LinearLayout。

那么,为什么当我点击 LinearLayouts 时 Videoview 会捕捉到触摸事件?

这是我的onTouch 实现:

@Override
    public boolean onTouch(View v, MotionEvent event) {

        if (!isMediaControlerShown) {
            topPanel.setVisibility(View.VISIBLE);
            bottomPanel.setVisibility(View.VISIBLE);
            isMediaControlerShown = true;
        } else {
            topPanel.setVisibility(View.GONE);
            bottomPanel.setVisibility(View.GONE);
            isMediaControlerShown = false;
        }
        return false;
    }

以及XML布局的架构:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content" />

<!-- The top panel of the Video Player -->

    <LinearLayout
        android:visibility="gone"
        android:id="@+id/top_panel"
        style="@style/VideoTopPanel"
        android:orientation="vertical" >

        <!-- ////// -->
    </LinearLayout>

<!-- The bottom panel of the Video Player -->

    <LinearLayout
        android:visibility="gone"
        android:id="@+id/bottom_panel"
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/VideoBottomPanel"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:paddingBottom="@dimen/video_bottom_panel_padding_bottom"
        android:paddingTop="@dimen/video_bottom_panel_padding_top" >

       <!-- ////// -->
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

  • onTouch 返回true 而不是false,因为您总是在处理触摸事件。如果您返回false,它会向下级联到下一个级别,直到找到true
  • 感谢您的评论。它没有帮助。行为是这样的:在动作 ACTION_DOWN 时媒体控制器出现,而在 ACTION_UP 时消失。所以控制器的冲洗速度非常快。

标签: android android-layout android-intent android-emulator android-widget


【解决方案1】:

通过为持有控件的视图设置一个点击监听器解决了这个问题,但在 onClick() 上什么也不做 这样,视图接收到点击事件并没有消失。

【讨论】:

    猜你喜欢
    • 2012-06-06
    • 1970-01-01
    • 2017-01-27
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多