1. 程式人生 > >webview與js的相互互動

webview與js的相互互動

方案思路, 1.在點選圖片的時候呼叫本地的java方法並給出響應的圖片地址 2.本地獲得圖片地址後,開啟一個遮罩activity進行顯示和處理 第二步的實現很容易實現,關鍵是第一步的實現,在網頁中點選圖片不會呼叫本地的java程式碼。那麼我們需要給這個點選事件加上相應的js函式,讓點選事件呼叫的js函式來呼叫我們提前準備好的java函式,等我們捕獲到圖片的url剩下的就好處理了。 關鍵點就是給普通的html注入我們的js函式,讓圖片能夠響應點選並呼叫js函式,在通過js函式來呼叫我們的java函式。聽起來好像有點繞,不過也不難,下面我們用程式碼實現下 對java和js互動還不熟悉的同學,請參照前面的文章
這次例項的主要功能:點選圖片在新的activity中展示,對圖片能夠進行手勢操作,包括雙指縮放等 效果圖
載入webview的activity程式碼  
  1. package wst.webview;  
  2. import android.annotation.SuppressLint;  
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.graphics.Bitmap;  
  7. import android.os.Bundle;  
  8. import android.webkit.WebView;  
  9. import android.webkit.WebViewClient;  
  10. @SuppressLint("SetJavaScriptEnabled")  
  11. publicclass MainActivity extends Activity {  
  12.     private WebView contentWebView = null;  
  13.     @SuppressLint("SetJavaScriptEnabled")  
  14.     @Override
  15.     publicvoid onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         contentWebView = (WebView) findViewById(R.id.webview);  
  19.         // 啟用javascript
  20.         contentWebView.getSettings().setJavaScriptEnabled(true);  
  21.         // 隨便找了個帶圖片的網站
  22.         contentWebView.loadUrl("http://www.weim.me/12408.html");  
  23.         // 新增js互動介面類,並起別名 imagelistner
  24.         contentWebView.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");  
  25.         contentWebView.setWebViewClient(new MyWebViewClient());  
  26.     }  
  27.     // 注入js函式監聽
  28.     privatevoid addImageClickListner() {  
  29.         // 這段js函式的功能就是,遍歷所有的img幾點,並新增onclick函式,函式的功能是在圖片點選的時候呼叫本地java介面並傳遞url過去
  30.         contentWebView.loadUrl("javascript:(function(){" +  
  31.         "var objs = document.getElementsByTagName(\"img\"); " +   
  32.                 "for(var i=0;i<objs.length;i++)  " +   
  33.         "{"
  34.                 + "    objs[i].onclick=function()  " +   
  35.         "    {  "
  36.                 + "        window.imagelistner.openImage(this.src);  " +   
  37.         "    }  " +   
  38.         "}" +   
  39.         "})()");  
  40.     }  
  41.     // js通訊介面
  42.     publicclass JavascriptInterface {  
  43.         private Context context;  
  44.         public JavascriptInterface(Context context) {  
  45.             this.context = context;  
  46.         }  
  47.         publicvoid openImage(String img) {  
  48.             System.out.println(img);  
  49.             //
  50.             Intent intent = new Intent();  
  51.             intent.putExtra("image", img);  
  52.             intent.setClass(context, ShowWebImageActivity.class);  
  53.             context.startActivity(intent);  
  54.             System.out.println(img);  
  55.         }  
  56.     }  
  57.     // 監聽
  58.     privateclass MyWebViewClient extends WebViewClient {  
  59.         @Override
  60.         publicboolean shouldOverrideUrlLoading(WebView view, String url) {  
  61.             returnsuper.shouldOverrideUrlLoading(view, url);  
  62.         }  
  63.         @Override
  64.         publicvoid onPageFinished(WebView view, String url) {  
  65.             view.getSettings().setJavaScriptEnabled(true);  
  66.             super.onPageFinished(view, url);  
  67.             // html載入完成之後,新增監聽圖片的點選js函式
  68.             addImageClickListner();  
  69.         }  
  70.         @Override
  71.         publicvoid onPageStarted(WebView view, String url, Bitmap favicon) {  
  72.             view.getSettings().setJavaScriptEnabled(true);  
  73.             super.onPageStarted(view, url, favicon);  
  74.         }  
  75.         @Override
  76.         publicvoid onReceivedError(WebView view, int errorCode, String description, String failingUrl) {  
  77.             super.onReceivedError(view, errorCode, description, failingUrl);  
  78.         }  
  79.     }  
  80. }  

展示圖片的activity程式碼
  1. package wst.webview;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.net.URL;  
  5. import android.app.Activity;  
  6. import android.graphics.drawable.BitmapDrawable;  
  7. import android.graphics.drawable.Drawable;  
  8. import android.os.Bundle;  
  9. import android.widget.TextView;  
  10. publicclass ShowWebImageActivity extends Activity {  
  11.     private TextView imageTextView = null;  
  12.     private String imagePath = null;  
  13.     private ZoomableImageView imageView = null;  
  14.     @Override
  15.     protectedvoid onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.show_webimage);  
  18.         this.imagePath = getIntent().getStringExtra("image");  
  19.         this.imageTextView = (TextView) findViewById(R.id.show_webimage_imagepath_textview);  
  20.         imageTextView.setText(this.imagePath);  
  21.         imageView = (ZoomableImageView) findViewById(R.id.show_webimage_imageview);  
  22.         try {  
  23.             imageView.setImageBitmap(((BitmapDrawable) ShowWebImageActivity.loadImageFromUrl(this.imagePath)).getBitmap());  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28.     publicstatic Drawable loadImageFromUrl(String url) throws IOException {  
  29.         URL m = new URL(url);  
  30.         InputStream i = (InputStream) m.getContent();  
  31.         Drawable d = Drawable.createFromStream(i, "src");  
  32.         return d;  
  33.     }  
  34. }  


圖片佈局檔案 
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical">
  6.     <!-- TODO 預設佔位圖 -->
  7.     <wst.webview.ZoomableImageView
  8.         android:id="@+id/show_webimage_imageview"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="fill_parent"
  11.         android:scaleType="matrix"
  12.         android:src="@drawable/icon"/>
  13.     <TextView
  14.         android:id="@+id/show_webimage_imagepath_textview"
  15.         android:layout_width="fill_parent"
  16.         android:layout_height="wrap_content"
  17.         android:gravity="center"
  18.         android:textColor="#ffff0000"/>
  19. </LinearLayout>

希望對大家有所幫助 原始碼附上