视频在右侧被切断

时间:2013-04-29 08:08:34

标签: android video webview

我的左边是ListView,另外还有Webview的布局。

当我想在WebView中播放视频时,视频会在右侧被剪切。

Android版:4.0.4

布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <ListView
        android:layout_width="150dp"
        android:layout_height="match_parent" 
        android:background="#334455">
    </ListView>
    <org.apache.cordova.CordovaWebView
        android:id="@+id/reading_pane_rss_erss_erss_web_view_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

我的左边是ListView,另外还有Webview的布局。

当我想在WebView中播放视频时,视频会在右侧被剪切。

Android版:4.0.4 活动代码:

public class VideoActivityNew extends Activity implements CordovaInterface{
    private CordovaWebView mWebView;
    private FrameLayout mCustomViewContainer;
    private View mCustomView;
    private CustomViewCallback mCustomViewCallback;
    private FrameLayout mFullscreenContainer;   
    protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
            new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    private View mVideoProgressView = null;
    private ViewFlipper mViewFlipper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mCustomViewContainer = new FrameLayout(getApplicationContext());
        mCustomViewContainer.setVisibility(View.GONE);

        mWebView = new CordovaWebView(this);
        mWebView = (CordovaWebView) findViewById(R.id.reading_pane_rss_erss_erss_web_view_container);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setFixedFontFamily("DroidSansHebrew.ttf");
        mWebView.getSettings().setPluginState(PluginState.ON);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        mWebView.getSettings().setSupportZoom(true);

        mWebView.setInitialScale(1);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        mWebView.setScrollbarFadingEnabled(true);
        mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        mWebView.getSettings().setBuiltInZoomControls(true);
        if (Build.VERSION.SDK_INT >= 11)
            mWebView.getSettings().setDisplayZoomControls(false);



        mWebView.setWebChromeClient(new CordovaChromeClient((CordovaInterface) this) {
            FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new   
                    FrameLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

            @Override
            public void onShowCustomView(View view, CustomViewCallback callback) {

                super.onShowCustomView(view, callback);
                showCustomView(view, callback);
            }

            @Override
            public void onHideCustomView() {
                hideCustomView();
            }


            @Override
            public View getVideoLoadingProgressView() {
                if (mVideoProgressView == null) {
                    LayoutInflater inflater = LayoutInflater.from(VideoActivityNew.this);
                    mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null);
                }
                return mVideoProgressView;
            }

        });

        mWebView.setWebViewClient(new CordovaWebViewClient(this, mWebView)
        {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("about:blank")) {
                    return true;
                }
                mWebView.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                mWebView.scrollTo(0, 0);
                // mWebProgressBar.setVisibility(View.INVISIBLE);
            }

        });
        String data = "<html dir=\"ltr\">\n" + 
                "<head>\n" + 
                "</head>\n" + 
                "<body>\n" + 
                "<div class=rumbleitem><font></font></div>\n" + 
                "<div class=rumbleitem><font><script type=\"text/javascript\" src=\"http://admin.brightcove.com/js/BrightcoveExperiences.js\"></script>\n" + 
                "<iframe class=\"youtube\" id=\"ytplayer\" type=\"text/html\" width=\"588\" height=\"392\"src=\"http://www.youtube.com/embed/dCzI521sgqE?fs=1&amp;wmode=opaque&amp;showinfo=0\" frameborder=\"0\"></iframe>\n" + 
                "<div class=\"dek\">\n" + 
                "<p style1=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12px;\">Democrats and Republicans can agree on one thing: Kevin Spacey. The White House Correspondents' Dinner began with a spoof of the hit series 'House of Cards,’ as Spacey reprised his role—while politicians from both sides of the aisle made cameos. As Spacey's Frank Underwood says, 'That's what real bipartisanship looks like.'<br>\n" + 
                "</p>\n" + 
                "<p style1=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12px;\"></p>\n" + 
                "</div>\n" + 
                "</font>\n" + 
                "</div>\n" + 
                "</body>\n" + 
                "</html>"; 
        mWebView.loadData(data, "text/html", "utf-8");

    }

    private void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
        if (mCustomView != null) {
            callback.onCustomViewHidden();
            return;
        }
        VideoActivityNew.this.getWindow().getDecorView();
        FrameLayout decor = (FrameLayout) getWindow().getDecorView();
        mFullscreenContainer = new FullscreenHolder(VideoActivityNew.this);
        mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
        decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
        mCustomView = view;
        setStatusBarVisibility(false);
        mCustomViewCallback = callback;
    }

    private void hideCustomView() {
        if (mCustomView == null)
            return;

        setStatusBarVisibility(true);
        FrameLayout decor = (FrameLayout) getWindow().getDecorView();
        decor.removeView(mFullscreenContainer);
        mFullscreenContainer = null;
        mCustomView = null;
        mCustomViewCallback.onCustomViewHidden();
    }

    @Override
    public void cancelLoadUrl() {
        // TODO Auto-generated method stub

    }
    @Override
    public Activity getActivity() {
        // TODO Auto-generated method stub
        return this;
    }
    @Override
    public Context getContext() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object onMessage(String arg0, Object arg1) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setActivityResultCallback(IPlugin arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void startActivityForResult(IPlugin arg0, Intent arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mCustomView != null) hideCustomView();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    static class FullscreenHolder extends FrameLayout {

        public FullscreenHolder(Context ctx) {
            super(ctx);
            setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
        }

        @Override
        public boolean onTouchEvent(MotionEvent evt) {
            return true;
        }
    }

    private void setStatusBarVisibility(boolean visible) {
        int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,通过重建像我这样的listviev来解决这个问题

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >


<LinearLayout android:id="@+id/thumbnail" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="3dip"      
    android:layout_alignParentLeft="true"
    android:background="@drawable/image_bg" 
    android:layout_marginRight="5dip">

    <ImageView     
        android:id="@+id/list_image"   
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/rihanna"/>

</LinearLayout>

<!-- Title Of Song-->
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Rihanna Love the way lie"
    android:textColor="#040404"
    android:typeface="sans" 
    android:textSize="15dip"
    android:textStyle="bold"/>

<!-- Artist Name -->
<TextView
    android:id="@+id/artist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Just gona stand there and ..." />

<!-- Rightend Duration -->
<TextView
    android:id="@+id/duration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/title"
    android:gravity="right"
    android:text="5:45"
    android:layout_marginRight="5dip"
    android:textSize="10dip"
    android:textColor="#10bcc9"
    android:textStyle="bold"/>

 <!-- Rightend Arrow -->    
 <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"/>

 <TextView
     android:id="@+id/track_url"
     android:layout_width="fill_parent"
     android:layout_height="18dip"
     android:layout_alignBottom="@+id/thumbnail"
     android:layout_alignParentRight="true"
     android:layout_toRightOf="@+id/thumbnail"
     android:textSize="1sp" />

 <TextView
     android:id="@+id/androidi_activity"
     android:layout_width="fill_parent"
     android:layout_height="1dip"
     android:layout_alignBottom="@+id/track_url"
     android:layout_alignParentRight="true"
     android:layout_toRightOf="@+id/thumbnail"
     android:textSize="1dip" />

</RelativeLayout>