版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/weixin_43802738/article/details/84540661
此篇文章属个人查阅资料整理所著,希望能对您有所帮助,欢迎各位留言指正,抱拳了
一、 首先在res文件夹下添加raw文件夹并将要添加的背景视频放进去;
二、在MyViewpager.java(此为要显示的活动文件)中的onCreate()中添加视频的代码
//设置视频背景
final VideoView videoview=(VideoView)findViewById(R.id.video_background);
final String videopath = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.entrance_video).toString();
videoview.setVideoPath(videopath);
videoview.start();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
mediaPlayer.setLooping(true);
}
});
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoview.setVideoPath(videopath);
videoview.start();
}
});
三、在layout文件夹下新建一个video_background.xml布局,然后在此布局内添加VideoView控件,此时可能视频不会填充满,只要将布局改为RelativeLayout就可以了;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--MyViewpager中include引用的样式-->
<!--最后四个属性使视屏全屏-->
<VideoView
android:id="@+id/video_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="center_vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
四、activity_login.xml中添加对video_background.xml的引用
<!--activity_login.xml中添加对video_background.xml的引用-->
<include layout="@layout/video_background"/>
五、在AndroidManifest.xml中注册MyViewpager.java(此为要显示的界面,即活动文件)
<activity android:name=".MyViewpager">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
效果如下:
视频截图来源于个人素材截取