麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

webview進(jìn)度條設(shè)置之WebChromeClient常用API與功能使用詳解

2019-11-09 14:13:26
字體:
供稿:網(wǎng)友

轉(zhuǎn)載自原文地址

0. 寫在前面

在使用這個(gè)WebChromeClient設(shè)置進(jìn)入條有一點(diǎn)要注意,因?yàn)檫@進(jìn)度加載不止一種樣式,我們設(shè)置PRogress時(shí),這個(gè)控件默認(rèn)的style是那種轉(zhuǎn)圈的,不仔細(xì)看可能看不清,以為沒設(shè)置上,所以我們一般可以改變這個(gè)控件的樣式,一般都是這種最經(jīng)典的,就是經(jīng)度條,代碼就是在progress中加一條style屬性。就這點(diǎn)要注意。
 style="?android:attr/progressBarStyleHorizontal" 

在WebView的開發(fā)過程中當(dāng)你需要使用到一些高級功能可以通過設(shè)置WebChromeClient從而來輔助WebView處理 javaScript 的對話框、網(wǎng)站圖標(biāo)、網(wǎng)站title、加載進(jìn)度等。

WebChromeClient常用的API方法
1.通知應(yīng)用程序當(dāng)前網(wǎng)頁加載的進(jìn)度
12
@Overridepublic void onProgressChanged(WebView view, int newProgress)
2.獲取網(wǎng)頁title標(biāo)題
12
@Overridepublic void onReceivedTitle(WebView view, String title)

獲取標(biāo)題的時(shí)間主要取決于網(wǎng)頁前段設(shè)置標(biāo)題的位置,一般設(shè)置在頁面加載前面,可以較早調(diào)用到這個(gè)函數(shù)

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

對應(yīng)的取消全屏方法

12
@Overridepublic void onHideCustomView()

WebView下載監(jiān)聽

通過設(shè)置webview下載監(jiān)聽進(jìn)而監(jiān)聽網(wǎng)頁下載

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

一般可在downloadStart 處進(jìn)行下載處理

WebChromeClient高級功能實(shí)現(xiàn)
1.讓你的webview支持File Input 標(biāo)簽

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

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;}

在選擇完圖片后回調(diào)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.支持全屏視頻播放

設(shè)置webview視頻未播放時(shí)默認(rèn)顯示占位圖

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

視頻播放全屏?xí)r調(diào)用

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);}

視頻取消全屏?xí)r候調(diào)用

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;}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 无码av女优| 99成人精品视频 | 日本免费aaa观看 | 国产日韩在线观看视频 | 免费黄色在线 | 精品国产一区二区三区在线 | 嗯啊羞羞视频 | 免费午夜视频 | 日韩视频www| 久草亚洲视频 | 久久草在线视频国产 | aaaaa国产欧美一区二区 | 一级尻逼视频 | 羞羞视频免费视频欧美 | 久草在线视频看看 | 毛片在线免费播放 | 伊人在线视频 | 亚洲第一成人在线视频 | 日韩字幕在线 | 欧美成人se01短视频在线看 | 久久久久一区二区三区 | 亚洲日韩中文字幕一区 | 欧美中文日韩 | 91精品福利视频 | 九色在线78m | 日韩视频在线观看免费视频 | 日韩黄色免费电影 | 爱看久久| 成人午夜视频在线观看免费 | www.91sese| 日本网站在线播放 | 久久久久久久一区二区 | 久草在线资源视频 | 久草导航 | 91香蕉国产亚洲一区二区三区 | 国产电影精品久久 | 精品一区二区三区电影 | 成人免费自拍视频 | 羞羞网站 | 久久久久国产精品久久久久 | 极品国产91在线网站 |