2014-07-23 204 views
0

我已經實現了一個web視圖來播放我的應用程序中嵌入的flv視頻。它的工作原理!但問題是視頻幀太小..(視頻只需要大約1/8的屏幕)如何讓我的視頻適合屏幕?如何讓我的網頁瀏覽適合屏幕?

這裏是我的代碼

MainActivity.java

package com.example.webvideo; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 

public class MainActivity extends Activity { 

private WebView mWebView; 
private LinearLayout mContentView; 
private FrameLayout mCustomViewContainer; 
private WebChromeClient.CustomViewCallback mCustomViewCallback; 
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
     ViewGroup.LayoutParams.WRAP_CONTENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mContentView = (LinearLayout) findViewById(R.id.linearlayout); 
    mWebView = (WebView) findViewById(R.id.webView); 
    mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content); 

    WebSettings webSettings = mWebView.getSettings(); 
    webSettings.setPluginState(WebSettings.PluginState.ON); 
    webSettings.setJavaScriptEnabled(true); 
    webSettings.setUseWideViewPort(true); 
    webSettings.setLoadWithOverviewMode(true); 

    mWebView.loadUrl("http://www.fortunagate.com/adaderana_apps/video_player.php?video=http://derana.lk/content/video/SriGauthamaSambuddha24-15thJune2014.flv"); 
    mWebView.setWebViewClient(new HelloWebViewClient()); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 



private class HelloWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView webview, String url) 
    { 
     webview.setWebChromeClient(new WebChromeClient() { 

      private View mCustomView; 

      @Override 
      public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) 
      { 
       // if a view already exists then immediately terminate the new one 
       if (mCustomView != null) 
       { 
        callback.onCustomViewHidden(); 
        return; 
       } 

       // Add the custom view to its container. 
       mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER); 
       mCustomView = view; 
       mCustomViewCallback = callback; 

       // hide main browser view 
       mContentView.setVisibility(View.GONE); 

       // Finally show the custom view container. 
       mCustomViewContainer.setVisibility(View.VISIBLE); 
       mCustomViewContainer.bringToFront(); 
      } 

     }); 

     webview.loadUrl(url); 

     return true; 
    } 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) 
    { 
     mWebView.goBack(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 

} 
} 

activity_main.xml中

<RelativeLayout 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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<FrameLayout 
      android:id="@+id/fullscreen_custom_content" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#FF000000"/> 

<LinearLayout 
      android:id="@+id/linearlayout" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

    <WebView 
      android:id="@+id/webView" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

</LinearLayout> 
</RelativeLayout> 

回答

0

我想你應該有你的WebView這樣的:

<RelativeLayout 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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <FrameLayout 
     android:id="@+id/fullscreen_custom_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FF000000"/> 

    <WebView 
     android:id="@+id/webView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:visibility="gone" /> 


</RelativeLayout> 

然後,當你wan。時只是將visibility更改爲visible,它將覆蓋整個屏幕。

+0

你什麼意思是'當你想要'。我不應該只是將可見性設置爲可見並使用? – user3852473

+0

你想在這種情況下Webview立即覆蓋整個屏幕?如果你這樣做,爲什麼你在同一個佈局文件中有一個FrameLayout? – luiscosta

+0

雖然我刪除了FrameworkLayout,但視頻在小玩家身上播放。 – user3852473