webview與js的相互互動
阿新 • • 發佈:2019-01-22
方案思路,
1.在點選圖片的時候呼叫本地的java方法並給出響應的圖片地址
2.本地獲得圖片地址後,開啟一個遮罩activity進行顯示和處理
第二步的實現很容易實現,關鍵是第一步的實現,在網頁中點選圖片不會呼叫本地的java程式碼。那麼我們需要給這個點選事件加上相應的js函式,讓點選事件呼叫的js函式來呼叫我們提前準備好的java函式,等我們捕獲到圖片的url剩下的就好處理了。
關鍵點就是給普通的html注入我們的js函式,讓圖片能夠響應點選並呼叫js函式,在通過js函式來呼叫我們的java函式。聽起來好像有點繞,不過也不難,下面我們用程式碼實現下
對java和js互動還不熟悉的同學,請參照前面的文章
這次例項的主要功能:點選圖片在新的activity中展示,對圖片能夠進行手勢操作,包括雙指縮放等
效果圖
載入webview的activity程式碼
展示圖片的activity程式碼
圖片佈局檔案
希望對大家有所幫助 原始碼附上
載入webview的activity程式碼
- package wst.webview;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- @SuppressLint("SetJavaScriptEnabled")
- publicclass MainActivity extends Activity {
- private WebView contentWebView = null;
- @SuppressLint("SetJavaScriptEnabled")
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- contentWebView = (WebView) findViewById(R.id.webview);
- // 啟用javascript
- contentWebView.getSettings().setJavaScriptEnabled(true);
- // 隨便找了個帶圖片的網站
- contentWebView.loadUrl("http://www.weim.me/12408.html");
- // 新增js互動介面類,並起別名 imagelistner
- contentWebView.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");
- contentWebView.setWebViewClient(new MyWebViewClient());
- }
- // 注入js函式監聽
- privatevoid addImageClickListner() {
- // 這段js函式的功能就是,遍歷所有的img幾點,並新增onclick函式,函式的功能是在圖片點選的時候呼叫本地java介面並傳遞url過去
- contentWebView.loadUrl("javascript:(function(){" +
- "var objs = document.getElementsByTagName(\"img\"); " +
- "for(var i=0;i<objs.length;i++) " +
- "{"
- + " objs[i].onclick=function() " +
- " { "
- + " window.imagelistner.openImage(this.src); " +
- " } " +
- "}" +
- "})()");
- }
- // js通訊介面
- publicclass JavascriptInterface {
- private Context context;
- public JavascriptInterface(Context context) {
- this.context = context;
- }
- publicvoid openImage(String img) {
- System.out.println(img);
- //
- Intent intent = new Intent();
- intent.putExtra("image", img);
- intent.setClass(context, ShowWebImageActivity.class);
- context.startActivity(intent);
- System.out.println(img);
- }
- }
- // 監聽
- privateclass MyWebViewClient extends WebViewClient {
- @Override
- publicboolean shouldOverrideUrlLoading(WebView view, String url) {
- returnsuper.shouldOverrideUrlLoading(view, url);
- }
- @Override
- publicvoid onPageFinished(WebView view, String url) {
- view.getSettings().setJavaScriptEnabled(true);
- super.onPageFinished(view, url);
- // html載入完成之後,新增監聽圖片的點選js函式
- addImageClickListner();
- }
- @Override
- publicvoid onPageStarted(WebView view, String url, Bitmap favicon) {
- view.getSettings().setJavaScriptEnabled(true);
- super.onPageStarted(view, url, favicon);
- }
- @Override
- publicvoid onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
- super.onReceivedError(view, errorCode, description, failingUrl);
- }
- }
- }
展示圖片的activity程式碼
- package wst.webview;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import android.app.Activity;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.widget.TextView;
- publicclass ShowWebImageActivity extends Activity {
- private TextView imageTextView = null;
- private String imagePath = null;
- private ZoomableImageView imageView = null;
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.show_webimage);
- this.imagePath = getIntent().getStringExtra("image");
- this.imageTextView = (TextView) findViewById(R.id.show_webimage_imagepath_textview);
- imageTextView.setText(this.imagePath);
- imageView = (ZoomableImageView) findViewById(R.id.show_webimage_imageview);
- try {
- imageView.setImageBitmap(((BitmapDrawable) ShowWebImageActivity.loadImageFromUrl(this.imagePath)).getBitmap());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- publicstatic Drawable loadImageFromUrl(String url) throws IOException {
- URL m = new URL(url);
- InputStream i = (InputStream) m.getContent();
- Drawable d = Drawable.createFromStream(i, "src");
- return d;
- }
- }
圖片佈局檔案
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <!-- TODO 預設佔位圖 -->
- <wst.webview.ZoomableImageView
- android:id="@+id/show_webimage_imageview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scaleType="matrix"
- android:src="@drawable/icon"/>
- <TextView
- android:id="@+id/show_webimage_imagepath_textview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textColor="#ffff0000"/>
- </LinearLayout>
希望對大家有所幫助 原始碼附上