Android WebView优化

 

 Android webview用户体验优化-场景恢复

分类: webview Android   329人阅读  评论(1)  收藏  举报

上一篇文章讲到了webview技术方面的优化(没有看到的朋友可以看看http://blog.csdn.net/fkingu007/article/details/44650031),这次进一步完善一下,主要完成状态保留工作,也是解决了我的一个疑问,下面看看如何做到的。


转载请标明文章出处 http://blog.csdn.net/fkingu007/article/details/44675129,尊重原创。


1、  activity意外被杀

       上个博文已经说到了,在onSaveInstanceState(Bundle)调用webview.saveState(bundle)保存状态,在onCreate(Bundle saveInstanceState)里通过savedInstanceState == null判断,不为null,即可通过webview.restoreState(bundle)恢复,这里不多讲了。


2、用户正常back

      我们都知道back是不会触发onSaveInstanceState()方法的,那么我们就得通过其他途径保存状态了,哈哈,研究发现可以通过webview.getContentHeight(),webview.getSrollY()得到滚动位置所占html页面实际内容长度的比例,由于html加载内容可能显示不完全,getContentHeight()的值很有可能是会变化的,所以我们最好算出这个百分比,下次恢复的时候也根据百分比再滚动,思路有了,代码实现就没问题啦,下面是关键代码:


[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. // 计算当前滚动位置所占网页内容的百分比  
  2.     private float calculateProgression(WebView content) {  
  3.         float positionTopView = content.getTop();  
  4.         float contentHeight = content.getContentHeight();  
  5.         float currentScrollPosition = content.getScrollY();  
  6.         float percentWebview = (currentScrollPosition - positionTopView) / contentHeight;  
  7.         Log.e(TAG, "positionTopView:"+positionTopView);  
  8.         Log.e(TAG, "contentHeight:"+contentHeight);  
  9.         Log.e(TAG, "currentScrollPosition:"+currentScrollPosition);  
  10.         Log.e(TAG, "percentWebview:"+percentWebview);  
  11.         return percentWebview;  
  12.     }  

onDestory()里保留一下这个值:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     protected void onDestroy() {  
  3.         mProgressToRestore = calculateProgression(wv);  
  4.         ll.removeAllViews();  
  5.         wv.stopLoading();  
  6.         wv.removeAllViews();  
  7.         wv.destroy();  
  8.         wv = null;  
  9.         ll = null;  
  10.         super.onDestroy();  
  11.     }  

WebViewClient.onPageFinished()里进行滚动,加一个标志位只跳转一次


[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.             public void onPageFinished(WebView view, String url) {  
  3.                 // TODO Auto-generated method stub  
  4.                   
  5.                 if (mHasToRestoreState && 0<mProgressToRestore) {  
  6.                     mHasToRestoreState = false;  
  7.                     view.postDelayed(new Runnable() {  
  8.                         @Override  
  9.                         public void run() {  
  10.                             float webviewsize = wv.getContentHeight() - wv.getTop();  
  11.                             float positionInWV = webviewsize * mProgressToRestore;  
  12.                             int positionY = (int) (wv.getTop() + positionInWV);  
  13.                             wv.scrollTo(0, positionY);  
  14.                         }  
  15.                     // 延迟一下  
  16.                     }, 100);  
  17.                 }  
  18.                   
  19.                 super.onPageFinished(view, url);  
  20.                 Log.d(TAG, "pageFinished:"+url);  
  21.                   
  22.             }  



就是这么做到的,大家可以试试效果。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值