前文:
桃詞典 Peach Dictionary 簡易英語詞典app開發 安卓軟體開發 Part 1
導航:
桃詞典 Peach Dictionary 簡易英語詞典app開發 安卓軟體開發 The End 導航頁及收尾作業
2.視頻背景
(1)在res檔案夾下新建raw檔案夾,

(2)將準備好的視頻素材放入raw檔案夾,

(3)進行UI布局和代碼處理,
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<VideoView
android:id="@+id/video_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:alpha="1"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.peachdictionary;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//設定此界面為豎屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
videobackground();
}
private void videobackground() {
//視頻背景
final VideoView videoview = findViewById(R.id.video_background);
final String videopath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.background).toString();
videoview.setVideoPath(videopath);
videoview.start();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setLooping(true);
}
});
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoview.setVideoPath(videopath);
videoview.start();
}
});
}
}
實作效果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/386640.html
標籤:其他
