我们看到市场上视频播放应用在播放视频时,可以在视频上面显示滚动字幕信息,或者其它提示图片信息等.那么它们是如何实现的呢,首先播放视频一般是用SurfaceView,VideoView等,那么我们如何在SurfaceView上显示字幕呢,根据我前面一篇文章所述SurfaceView的工作原理,SurfaceView的生命周期虽然受到它的宿主窗口限制,但是它是工作于不同的窗口,且位于宿主窗口之下.我们直接看个例子.
对应activity_main.xml布局如下:
<FrameLayout 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"> <VideoView android:id="@+id/surface_view" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/toast_info" android:layout_width="600dp" android:layout_height="100dip" android:gravity="center" android