我接著上一篇的總結(jié)繼續(xù)寫(xiě)藻肄,上一篇已經(jīng)可以正常加載網(wǎng)頁(yè)并且做基本控制,不過(guò)我發(fā)現(xiàn)一個(gè)問(wèn)題,播放網(wǎng)頁(yè)視頻的時(shí)候無(wú)法進(jìn)入全屏模式進(jìn)行視頻播放或舞,體驗(yàn)極差,原來(lái)這里是我們自己需要手動(dòng)對(duì)全屏模式進(jìn)行處理的蒙幻。這篇主要寫(xiě)WwebView實(shí)現(xiàn)全屏播放映凳,還有記錄一下在處理全屏播放時(shí)候遇到的一個(gè)問(wèn)題。
1.思考
播放視頻的時(shí)候點(diǎn)擊全屏圖標(biāo)點(diǎn)擊后木有反應(yīng)邮破,但其實(shí)是觸發(fā)了WebChromeClient
類中onShowCustomView(View view, CustomViewCallback callback)
事件的诈豌,注意這個(gè)方法里面的兩個(gè)參數(shù)view
和callback
,view
就是傳過(guò)來(lái)的視頻播放的視圖內(nèi)容抒和,callback
回調(diào)接口看源代碼可知里面有一個(gè)方法onCustomViewHidden()
注釋給的解釋是(Invoked when the host application dismisses the)
矫渔,意思是視圖關(guān)閉的時(shí)候調(diào)用這個(gè)onCustomViewHidden()
方法,退出全屏的時(shí)候會(huì)回調(diào) onHideCustomView()
無(wú)參數(shù)方法摧莽,得出結(jié)論:簡(jiǎn)單的來(lái)說(shuō)就是進(jìn)入全屏模式的時(shí)候把onShowCustomView
方法中的View添加到一個(gè)容器中顯示出來(lái)蚌斩,退出全屏模式時(shí)在onHideCustomView
方法中把視圖從容器remove掉就OK了,順便處理一下橫豎屏切換范嘱。
/**
* A callback interface used by the host application to notify
* the current page that its custom view has been dismissed.
*/
public interface CustomViewCallback {
/**
* Invoked when the host application dismisses the
* custom view.
*/
public void onCustomViewHidden();
}
2.開(kāi)啟硬件加速
為什么要開(kāi)啟硬件加速八蜕拧?因?yàn)椴婚_(kāi)啟的話會(huì)有視頻無(wú)法正常播放的問(wèn)題丑蛤,還有就是瀏覽網(wǎng)頁(yè)的時(shí)候更流暢叠聋。
在application節(jié)點(diǎn)中添加android:supportsRtl="true"
在activity中添加android:hardwareAccelerated="true"
,在application添加表示整個(gè)應(yīng)用程序開(kāi)啟,在activity添加表示當(dāng)前頁(yè)面開(kāi)啟受裹。
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:hardwareAccelerated="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
3.網(wǎng)頁(yè)全屏模式切換回調(diào)處理
layout布局中添加FrameLayout
作為全屏模式的視圖容器碌补;
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context="com.allyn.webview.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.ContentLoadingProgressBar
android:id="@+id/pro_schedule"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="3dp"
android:max="100"
android:progress="0"
android:visibility="visible" />
<com.allyn.webview.MyWebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="500dp" />
</LinearLayout>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
全屏模式切換回調(diào)處理
mWebView.setWebChromeClient(new WebChromeClient() {
//視圖View
private View mCustomView;
// 一個(gè)回調(diào)接口使用的主機(jī)應(yīng)用程序通知當(dāng)前頁(yè)面的自定義視圖已被撤職
private CustomViewCallback mCustomViewCallback;
//網(wǎng)頁(yè)進(jìn)入全屏模式監(jiān)聽(tīng)
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
mFrameLayout = findViewById(R.id.frameLayout);
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
//賦值虏束,關(guān)閉時(shí)需要調(diào)用
mCustomView = view;
// 將video放到FrameLayout中
mFrameLayout.addView(mCustomView);
// 退出全屏模式時(shí)釋放需要調(diào)用,這里先賦值
mCustomViewCallback = callback;
// 設(shè)置webView隱藏
mWebView.setVisibility(View.GONE);
//切換至橫屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
//網(wǎng)頁(yè)退出全屏模式監(jiān)聽(tīng)
@Override
public void onHideCustomView() {
//顯示豎屏?xí)r候的webview
mWebView.setVisibility(View.VISIBLE);
if (mCustomView == null) {
return;
}
//隱藏
mCustomView.setVisibility(View.GONE);
//從當(dāng)前視圖中移除
mFrameLayout.removeView(mCustomView);
//釋放自定義視圖
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
//切換至豎屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onHideCustomView();
}
});
3.APP橫豎屏切換處理
注意:需要在AndroidManifest.xml
文件中的activity
節(jié)點(diǎn)下添加 android:configChanges="orientation|screenSize|keyboardHidden"
厦章,這么設(shè)置是為了使橫豎屏切換后Activity不會(huì)重新啟動(dòng)镇匀。
//屏幕旋轉(zhuǎn)監(jiān)聽(tīng)
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
switch (config.orientation) {
//豎屏方向
case Configuration.ORIENTATION_LANDSCAPE:
//設(shè)置全屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
break;
//橫屏方向
case Configuration.ORIENTATION_PORTRAIT:
//關(guān)閉全屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
break;
}
}
到這里全屏播放的功能完成了,效果圖如下:
4.遇到的一個(gè)問(wèn)題
在優(yōu)酷網(wǎng)上點(diǎn)擊播放視頻的時(shí)候出現(xiàn)了錯(cuò)誤頁(yè)面袜啃,提示:
這個(gè)問(wèn)題是網(wǎng)址使用了外鏈導(dǎo)致的汗侵,需要在
shouldOverrideUrlLoading()
方法中對(duì)url做處理,判斷url的前綴進(jìn)行本地跳轉(zhuǎn)群发。
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//網(wǎng)址處理
if (url == null) return false;
//外鏈判斷處理
try {
if (url.startsWith("weixin://") //微信
|| url.startsWith("alipays://") //支付寶
|| url.startsWith("mailto://") //郵件
|| url.startsWith("tel://")//電話
|| url.startsWith("baiduhaokan://")//百度
|| url.startsWith("tbopen://")//百度+
|| url.startsWith("youku://")//優(yōu)酷
|| url.startsWith("dianping://")//大眾點(diǎn)評(píng)
//其他自定義的scheme
) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
} catch (Exception e) { //防止crash (如果手機(jī)上沒(méi)有安裝處理某個(gè)scheme開(kāi)頭的url的APP, 會(huì)導(dǎo)致crash)
return true;//沒(méi)有安裝該app時(shí)晰韵,返回true,表示攔截自定義鏈接熟妓,但不跳轉(zhuǎn)雪猪,避免彈出上面的錯(cuò)誤頁(yè)面
}
/**
* 可對(duì)指定網(wǎng)址進(jìn)行攔截
*/
view.loadUrl(url);
return true;
}
好了,webview實(shí)踐總結(jié)第二篇到這里寫(xiě)完了起愈,覺(jué)得有用的可以點(diǎn)個(gè)小心心支持一下只恨,(比心手勢(shì).png)
預(yù)告:第三篇寫(xiě)webview的圖片上傳與網(wǎng)頁(yè)長(zhǎng)按處理。