我正在Android應用中工作,我想在webView加載網頁時預加載初始屏幕但是我有一個本地.mp4視頻而不是圖片。在Android的視頻初始屏幕期間預加載WebView
所以一旦用戶點擊應用程序,.mp4將開始播放(4秒)。在這4秒內,webView應該預先加載網頁SO當視頻完成後顯示我的網頁(如果網頁已經加載),否則請等待啓動畫面,直到網頁準備就緒,然後加載它。
這裏是我的MainActivity:
public class MainActivity extends AppCompatActivity {
private WebView webView;
public static final Object SPLASH_LOCK = new Object();
@Override
protected void onCreate(Bundle savedInstanceState) {
String myURL = "https://www.testpage.com";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setAllowContentAccess(true);
/** tell the webView to enable javascript execution */
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webSettings.getAllowFileAccessFromFileURLs();
webSettings.getAllowUniversalAccessFromFileURLs();
/** Load the HTML page */
webView.loadUrl(myURL);
/** Call the JavaScriptInterface within the WebView */
webView.addJavascriptInterface(this, "jsinterface");
startActivity(new Intent(this, AnimationScreenActivity.class));
/** Enable Javascript in WebView
/callback for browser events */
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished (WebView webView, String url) {
synchronized (SPLASH_LOCK) {
SPLASH_LOCK.notifyAll();
}
}
});
}
}
這裏是AnimationScreenActivity:
public class AnimationScreenActivity extends AppCompatActivity{
private static String TAG = AnimationScreenActivity.class.getName();
private static long MAX_SPLASH_TIME = 10000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_screen);
try {
VideoView videoHolder = (VideoView) findViewById(R.id.videoView1);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myvideo);
videoHolder.setVideoURI(video);
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
jump();
}
});
videoHolder.start();
} catch (Exception ex) { jump(); }
}
private void jump() {
new Thread() {
@Override
public void run() {
synchronized (MainActivity.SPLASH_LOCK) {
// wait for notify or time-out
try {
MainActivity.SPLASH_LOCK.wait(MAX_SPLASH_TIME);
} catch (InterruptedException ignored) {}
}
finish();
}
}.start();
}
}
這裏是activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="test.test_android.MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
這裏是animation_screen_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/animation_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="test.test.AnimationScreenActivity">
<VideoView
android:id="@+id/videoView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
而且最後的Manifest.xml這裏我設置MainActivity爲LAUNCHER:
<activity android:name=".MainActivity"
android:theme="@style/FullScreenTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AnimationScreenActivity"
android:theme="@style/FullScreenTheme"
android:screenOrientation="portrait"/>
所以我直到現在是,一旦用戶啓動應用程序時,.MP4開始當.MP4完成那麼它會在AnimationScreenActivity中等待10秒,然後加載網頁,然後再等待THEN。
任何幫助將不勝感激!
感謝您的回答! 'onPageFinished'不會被多次調用。我在Manifest中改變方向的原因是因爲我希望它始終處於肖像模式。另外,如果我使用ViewSwitcher或ViewAnimator,這將如何解決飛濺(動畫)期間加載網頁的問題? 是否有可能提供我的代碼如何做到這一點? – Johny
所以我必須使用你提供的兩個代碼之一?我如何在代碼中使用它? – Johny
是的,它會正常工作。活動的佈局將ViewSwitcher作爲父視圖的父視圖:視圖之一和視頻之一。 –