Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自定義WebView打造炫酷引導頁(含頂部進度條)

編輯:關於 Android 程式設計

這次分享一個做法:用自定義的 WebView 承載 H5 引導頁,並在頂部疊一條隨加載進度變化的進度條。效果相當有質感,程式碼量卻不大。

MainActivity.java

Activity 這邊的事情很少:找到自定義 WebView、開啟 JavaScript、按需調整圖片加載策略,再處理返回鍵讓它能回退網頁歷史。

 package com.bzu.gxs.webview1;

 import android.app.Activity;
 import android.os.Build;
 import android.os.Bundle;
 import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.MenuItem;

 public class MainActivity extends Activity {

     private MyWebView myWebView;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         myWebView = (MyWebView) findViewById(R.id.webView);
         myWebView.getSettings().setJavaScriptEnabled(true);
         init();
         myWebView.loadUrl("https://h5.eqxiu.com/s/F93iW6fu");
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.my, menu);
         return true;
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         int id = item.getItemId();
         if (id == R.id.action_settings) {
             return true;
         }
         return super.onOptionsItemSelected(item);
     }

     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
             myWebView.goBack();
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }

     public void init() {
         if (Build.VERSION.SDK_INT >= 19) {
             myWebView.getSettings().setLoadsImagesAutomatically(true);
         } else {
             myWebView.getSettings().setLoadsImagesAutomatically(false);
         }
     }
 }

MyWebView.java:核心自定義邏輯

三件事:構造時把進度條 addView 進來;用 WebChromeClient.onProgressChanged 同步進度,滿 100 就隱藏;重寫 onScrollChanged 讓進度條位置跟著內容滾動。

 package com.bzu.gxs.webview1;

 import android.content.Context;
 import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import android.widget.ProgressBar;
 import android.widget.Toast;

 /**
  * Created by GXS on 2016/5/12.
  */
 public class MyWebView extends WebView {

     private ProgressBar progressBar;
     private Context mContext;

     public MyWebView(Context context, AttributeSet attributeSet) {
         super(context, attributeSet);
         mContext = context;
         progressBar = (ProgressBar) LayoutInflater.from(context)
                 .inflate(R.layout.progressbar, null);
         progressBar.setLayoutParams(
                 new LayoutParams(LayoutParams.MATCH_PARENT, 6, 0, 0));
         addView(progressBar);

         setWebChromeClient(new WebChromeClient());
         setWebViewClient(new WebViewClient() {
             @Override
             public void onReceivedError(WebView view, int errorCode,
                     String description, String failingUrl) {
                 Toast.makeText(mContext, "Gxs" + description,
                         Toast.LENGTH_SHORT).show();
             }
         });

         this.getSettings().setBuiltInZoomControls(true);
         this.getSettings().setUseWideViewPort(true);
     }

     public class WebChromeClient extends android.webkit.WebChromeClient {
         @Override
         public void onProgressChanged(WebView view, int newProgress) {
             if (newProgress == 100) {
                 progressBar.setVisibility(GONE);
             } else {
                 if (progressBar.getVisibility() == GONE) {
                     progressBar.setVisibility(VISIBLE);
                 }
                 progressBar.setProgress(newProgress);
             }
             super.onProgressChanged(view, newProgress);
         }
     }

     @Override
     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
         LayoutParams layoutParams = (LayoutParams) progressBar.getLayoutParams();
         layoutParams.x = l;
         layoutParams.y = t;
         progressBar.setLayoutParams(layoutParams);
         super.onScrollChanged(l, t, oldl, oldt);
     }
 }

佈局檔

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
     xmlns:tools="https://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="com.bzu.gxs.webview1.MainActivity">

     <com.bzu.gxs.webview1.MyWebView
         android:id="@+id/webView"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
 </RelativeLayout> 

progressbar.xml

 <?xml version="1.0" encoding="utf-8"?>
 <ProgressBar xmlns:android="https://schemas.android.com/apk/res/android"
     android:id="@+id/ProgressBar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:indeterminate="false"
     android:maxHeight="10dip"
     android:minHeight="10dip"
     android:progress="50"
     android:progressDrawable="@drawable/greenprogress" />

注意:別忘了在 AndroidManifest.xml 中加入 <uses-permission android:name="android.permission.INTERNET"/>。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved