android 闪屏页webview 开启硬件加速 播放视频时会闪屏 怎么解决。

WebView的onPause Android硬件加速以及WebView的onPause,onResume,OnDestroy - 为程序员服务
为程序员服务
Android硬件加速以及WebView的onPause,onResume,OnDestroy
MainActivity如下:
package cn.
import java.util.T
import java.util.TimerT
import android.os.B
import android.view.V
import android.view.ViewC
import android.webkit.WebS
import android.webkit.WebV
import android.webkit.WebViewC
import android.webkit.WebSettings.PluginS
import android.app.A
import android.graphics.B
* Demo描述:
* 1 硬件加速的实现
* 2 WebView的暂停/回复、销毁
* 参考资料:
* 1 /questions/5267639/how-to-safely-turn-webview-zooming-on-and-off-as-needed
* 2 http://qianxuechao./blog/static//
Thank you very much
public class MainActivity extends Activity {
private WebView mWebV
private boolean isOnPause =
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hardwareAccelerate();
initWebView();
//硬件加速
private void hardwareAccelerate(){
if (this.getPhoneSDKInt() &= 14) {
getWindow().setFlags(0xx1000000);
//设置WebView
private void initWebView() {
mWebView = (WebView) findViewById(R.id.webView);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.requestFocus();
//以下两句和硬件加速有关
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.loadUrl(&/&);
mWebView.setWebViewClient(new TestWebViewClient());
* 当Activity执行onPause()时让WebView执行pause
protected void onPause() {
super.onPause();
if (mWebView != null) {
mWebView.getClass().getMethod(&onPause&).invoke(mWebView, (Object[]) null);
isOnPause =
} catch (Exception e) {
e.printStackTrace();
* 当Activity执行onResume()时让WebView执行resume
protected void onResume() {
super.onResume();
if (isOnPause) {
if (mWebView != null) {
mWebView.getClass().getMethod(&onResume&).invoke(mWebView, (Object[]) null);
isOnPause =
} catch (Exception e) {
e.printStackTrace();
* 该处的处理尤为重要:
* 应该在内置缩放控件消失以后,再执行mWebView.destroy()
* 否则报错WindowLeaked
protected void onDestroy() {
super.onDestroy();
if (mWebView != null) {
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setVisibility(View.GONE);
long delayTime = ViewConfiguration.getZoomControlsTimeout();
new Timer().schedule(new TimerTask() {
public void run() {
mWebView.destroy();
mWebView =
}, delayTime);
isOnPause =
private class TestWebViewClient extends WebViewClient{
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
public int getPhoneSDKInt() {
int version = 0;
version = Integer.valueOf(android.os.Build.VERSION.SDK);
} catch (NumberFormatException e) {
e.printStackTrace();
main.xml如下:
&RelativeLayout
xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:id=&@+id/webView&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:text=&@string/hello_world&
android:layout_centerInParent=&true&
&/RelativeLayout&
您可能的代码
相关聚客文章
相关专栏文章android硬件加速后webview闪烁问题
android webview 在3.0+后显示flash要启用硬件加速,开启硬件加速是在manifest中加入: android:hardwareAccelerated=&true&
但是开启硬件加速后webview有可能会出现闪烁的问题,解决方法是在webview中设置:
&&&&&&& setLayerType(View.LAYER_TYPE_SOFTWARE, null);
这是把webview 中的硬件加速关闭。设置LAYER_TYPE_SOFTWARE后会把当前view转为bitmap保存。这样就不能开多个webview,否则会报out of memory。
解决方法是在webview中加入:
&&& protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
&&&&&&& invalidate();
&&&&&&&&&&& super.onMeasure(widthMeasureSpec, heightMeasureSpec);
摘自 fhy_2008的专栏}

我要回帖

更多关于 android 闪屏 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信