3
我正在关注一个教程,尝试通过Android的VideoView构建一个自定义视频播放器。
现在,当我触摸VideoView时,我想要显示或隐藏媒体控制器。如果控制器已经显示,然后隐藏它们,如果它们被隐藏,然后显示它们。Android - VideoView - onTouch的行为并不像预期的那样
这些媒体控制器由2个LinearLayouts表示,位于VideoView之上。 用红色突出显示的区域表示背景中的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布局scheleton:
<?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”。 – DeeV
感谢您的评论。它没有帮助。行为是这样的:在动作ACTION_DOWN媒体控制器出现,并在ACTION_UP消失。所以控制器非常快速地冲洗。 –