我正处于一个项目中,我正在处理大约10个嵌入了HTML5视频的网络视图。这些Web视图使用自定义适配器视图进行处理。 HTML5视频正常播放,并在网络视图中处理全屏我找到了一个工作链接
Playing HTML5 video on fullscreen in android webview
当我尝试使用Gallery时,此代码工作正常,但当我使用自己的自定义适配器视图时,整个屏幕都没有弹出。
在过去的一周里,我坚持了这一点。请帮助我。
这是我的详细代码
(MainActivity.java)
package com.example.testlayout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GalleryWebView g = new GalleryWebView(this);
g.setAdapter(new WebAdapter(this));
setContentView(g);
}
public class WebAdapter extends BaseAdapter{
private Context mContext;
public WebAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 10;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
WebPageView mWeb = new WebPageView(mContext);
return mWeb;
}
}
}
(GalleryWebView.java)
package com.example.testlayout;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
public class GalleryWebView extends AdapterView {
public GalleryWebView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
/** The adapter with all the data */
private Adapter mAdapter;
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// if we don't have an adapter, we don't need to do anything
if (mAdapter == null) {
return;
}
if (getChildCount() == 0 ) {
int position = 0;
int bottomEdge = 0;
while (bottomEdge < getHeight() && position < mAdapter.getCount()) {
View newBottomChild = mAdapter.getView(position, null, this);
addAndMeasureChild(newBottomChild);
bottomEdge += newBottomChild.getMeasuredHeight();
position++;
}
}
positionItems();
}
/**
* Adds a view as a child view and takes care of measuring it
*
* @param child The view to add
*/
private void addAndMeasureChild(View child) {
LayoutParams params = child.getLayoutParams();
if (params == null) {
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
addViewInLayout(child, -1, params, true);
int itemWidth = getWidth();
int itemHeight = getHeight();
child.measure(MeasureSpec.EXACTLY | itemWidth, MeasureSpec.EXACTLY | itemHeight);
}
/**
* Positions the children at the "correct" positions
*/
private void positionItems() {
int top = 0;
for (int index = 0; index< getChildCount(); index++) {
View child = getChildAt(index);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
int left = (getWidth() - width) / 2;
child.layout(left, top, left + width, top + height);
top += height;
}
}
@Override
public void setAdapter(Adapter adapter) {
mAdapter = adapter;
removeAllViewsInLayout();
requestLayout();
}
@Override
public Adapter getAdapter() {
return mAdapter;
}
@Override
public void setSelection(int position) {
throw new UnsupportedOperationException("Not supported");
}
@Override
public View getSelectedView() {
throw new UnsupportedOperationException("Not supported");
}
}
(WebPageView.java)
package com.example.testlayout;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
public class WebPageView extends FrameLayout {
private Context mContext;
private MyWebChromeClient mWebChromeClient;
private View mCustomView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private FrameLayout mContentView;
private FrameLayout mBrowserFrameLayout;
FrameLayout mLayout1;
private WebView web;
private void init(Context context) {
mContext = context;
mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(mContext).inflate(R.layout.custom_screen, null);
mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);
mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.fullscreen_custom_content);
web = (WebView) mBrowserFrameLayout.findViewById(R.id.web);
mWebChromeClient = new MyWebChromeClient();
web.setWebChromeClient(mWebChromeClient);
web.setWebViewClient(new MyWebViewClient());
// Configure the webview
WebSettings s = web.getSettings();
s.setBuiltInZoomControls(true);
s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
s.setUseWideViewPort(true);
s.setLoadWithOverviewMode(true);
s.setSavePassword(true);
s.setSaveFormData(true);
s.setJavaScriptEnabled(true);
// enable Web Storage: localStorage, sessionStorage
s.setDomStorageEnabled(true);
web.loadUrl("file:///mnt/sdcard/htmlad/htmlad/index.html");
addView(mBrowserFrameLayout);
}
public WebPageView(Context context) {
super(context);
init(context);
}
public WebPageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public WebPageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private class MyWebChromeClient extends WebChromeClient {
private Bitmap mDefaultVideoPoster;
private View mVideoProgressView;
@Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
web.setVisibility(View.GONE);
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
}
@Override
public void onHideCustomView() {
}
@Override
public Bitmap getDefaultVideoPoster() {
if (mDefaultVideoPoster == null) {
mDefaultVideoPoster = BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher);
}
return mDefaultVideoPoster;
}
@Override
public View getVideoLoadingProgressView() {
if (mVideoProgressView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null);
}
return mVideoProgressView;
}
@Override
public void onReceivedTitle(WebView view, String title) {
((Activity) mContext).setTitle(title);
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
((Activity) mContext).getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100);
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(LOGTAG, "shouldOverrideUrlLoading: "+url);
// don't override URL so that stuff within iframe can work properly
// view.loadUrl(url);
return false;
}
}
static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
(custom_screen.xml)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<FrameLayout
android:id="@+id/fullscreen_custom_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</FrameLayout>