Android開發,登入註冊介面中如何新增視訊背景,親測可用
阿新 • • 發佈:2018-12-23
此篇文章屬個人查閱資料整理所著,希望能對您有所幫助,歡迎各位留言指正,抱拳了
一、 首先在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>
效果如下:
視訊截圖來源於個人素材擷取