国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

webview進度條設置之WebChromeClient常用API與功能使用詳解

2019-11-09 14:13:26
字體:
來源:轉載
供稿:網友

轉載自原文地址

0. 寫在前面

在使用這個WebChromeClient設置進入條有一點要注意,因為這進度加載不止一種樣式,我們設置PRogress時,這個控件默認的style是那種轉圈的,不仔細看可能看不清,以為沒設置上,所以我們一般可以改變這個控件的樣式,一般都是這種最經典的,就是經度條,代碼就是在progress中加一條style屬性。就這點要注意。
 style="?android:attr/progressBarStyleHorizontal" 

在WebView的開發過程中當你需要使用到一些高級功能可以通過設置WebChromeClient從而來輔助WebView處理 javaScript 的對話框、網站圖標、網站title、加載進度等。

WebChromeClient常用的API方法
1.通知應用程序當前網頁加載的進度
12
@Overridepublic void onProgressChanged(WebView view, int newProgress)
2.獲取網頁title標題
12
@Overridepublic void onReceivedTitle(WebView view, String title)

獲取標題的時間主要取決于網頁前段設置標題的位置,一般設置在頁面加載前面,可以較早調用到這個函數

3.網頁中有H5播放Flash video的時候按下全屏按鈕將會調用到這個方法,一般用作設置網頁播放全屏操作
12
@Overridepublic void onShowCustomView(View view, CustomViewCallback callback)

對應的取消全屏方法

12
@Overridepublic void onHideCustomView()

WebView下載監聽

通過設置webview下載監聽進而監聽網頁下載

12345
mWebView.setDownloadListener(new DownloadListener() {    @Override    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {    }});

一般可在downloadStart 處進行下載處理

WebChromeClient高級功能實現
1.讓你的webview支持File Input 標簽

在Android 5.0 API 21后 借助新的 onShowFileChooser() 方法,您現在不但可以在 WebView 中使用輸入表單字段,而且可以啟動文件選擇器從 Android 設備中選擇圖片和文件

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,                                 WebChromeClient.FileChooserParams fileChooserParams) {    if (mFilePathCallback != null) {        mFilePathCallback.onReceiveValue(null);    }    mFilePathCallback = filePathCallback;    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {        // Create the File where the photo should go        File photoFile = null;        try {            photoFile = createImageFile();            takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);        } catch (IOException ex) {            // Error occurred while creating the File            Log.e(TAG, "Unable to create Image File", ex);        }        // Continue only if the File was successfully created        if (photoFile != null) {            mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,                    Uri.fromFile(photoFile));        } else {            takePictureIntent = null;        }    }    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);    contentSelectionIntent.setType("image/*");    Intent[] intentArray;    if (takePictureIntent != null) {        intentArray = new Intent[]{takePictureIntent};    } else {        intentArray = new Intent[0];    }    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);    startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);    return true;}

在選擇完圖片后回調onActivityResult 獲取圖片

12345678910111213141516171819202122232425262728
@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {    if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {        super.onActivityResult(requestCode, resultCode, data);        return;    }    Uri[] results = null;    // Check that the response is a good one    if (resultCode == Activity.RESULT_OK) {        if (data == null) {            // If there is not data, then we may have taken a photo            if (mCameraPhotoPath != null) {                results = new Uri[]{Uri.parse(mCameraPhotoPath)};            }        } else {            String dataString = data.getDataString();            if (dataString != null) {                results = new Uri[]{Uri.parse(dataString)};            }        }    }    mFilePathCallback.onReceiveValue(results);    mFilePathCallback = null;    return;}

2.支持全屏視頻播放

設置webview視頻未播放時默認顯示占位圖

123456789
@Overridepublic Bitmap getDefaultVideoPoster() {    if(getActivity() == null) {        return null;    }    return BitmapFactory.decodeResource(getActivity().getapplicationContext().getResources(),            R.drawable.video_poster);}

視頻播放全屏時調用

12345678910111213141516171819202122232425262728293031323334
@Overridepublic void onShowCustomView(View view,                             WebChromeClient.CustomViewCallback callback) {    // if a view already exists then immediately terminate the new one    if (mCustomView != null) {        onHideCustomView();        return;    }    // 1. Stash the current state    mCustomView = view;    mOriginalSystemUiVisibility = getActivity().getWindow().getDecorView().getSystemUiVisibility();    mOriginalOrientation = getActivity().getRequestedOrientation();    // 2. Stash the custom view callback    mCustomViewCallback = callback;    // 3. Add the custom view to the view hierarchy    FrameLayout decor = (FrameLayout) getActivity().getWindow().getDecorView();    decor.addView(mCustomView, new FrameLayout.LayoutParams(            ViewGroup.LayoutParams.MATCH_PARENT,            ViewGroup.LayoutParams.MATCH_PARENT));    // 4. Change the state of the window    getActivity().getWindow().getDecorView().setSystemUiVisibility(            View.SYSTEM_UI_FLAG_LAYOUT_STABLE |                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |                    View.SYSTEM_UI_FLAG_FULLSCREEN |                    View.SYSTEM_UI_FLAG_IMMERSIVE);    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}

視頻取消全屏時候調用

1234567891011121314151617
@Overridepublic void onHideCustomView() {    // 1. Remove the custom view    FrameLayout decor = (FrameLayout) getActivity().getWindow().getDecorView();    decor.removeView(mCustomView);    mCustomView = null;    // 2. Restore the state to it's original form    getActivity().getWindow().getDecorView()            .setSystemUiVisibility(mOriginalSystemUiVisibility);    getActivity().setRequestedOrientation(mOriginalOrientation);    // 3. Call the custom view callback    mCustomViewCallback.onCustomViewHidden();    mCustomViewCallback = null;}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 喀喇| 沅江市| 滦南县| 德保县| 达州市| 固安县| 壤塘县| 天长市| 白山市| 桃江县| 从化市| 陵川县| 吴江市| 芦溪县| 文成县| 邳州市| 大连市| 湖州市| 稻城县| 茌平县| 胶南市| 运城市| 克东县| 尤溪县| 青冈县| 南开区| 黄山市| 扶绥县| 聂拉木县| 本溪| 灵丘县| 望奎县| 唐山市| 红桥区| 广南县| 无为县| 西和县| 松江区| 沙河市| 浮山县| 清流县|