webview中的图片进行放大显示 android webview js交互, 响应webview中的图片点击事件
分类:手机开发| 发布:佚名| 查看: | 发表时间:2014/6/19
项目新需求需要点击webview中的图片进行放大显示。
整理了下思路,想到了下面的一个可行的方案。
方案思路,
1.在点击图片的时候调用本地的java方法并给出响应的图片地址
2.本地获得图片地址后,开启一个遮罩activity进行显示和处理
第二步的实现很容易实现,关键是第一步的实现,在网页中点击图片不会调用本地的java代码。那么我们需要给这个点击事件加上相应的js函数,让点击事件调用的js函数来调用我们提前准备好的java函数,等我们捕获到图片的url剩下的就好处理了。
关键点就是给普通的html注入我们的js函数,让图片能够响应点击并调用js函数,在通过js函数来调用我们的java函数。听起来好像有点绕,不过也不难,下面我们用代码实现下
这次实例的主要功能:点击图片在新的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")
- public class MainActivity extends Activity {
-
- private WebView contentWebView = null;
-
- @SuppressLint("SetJavaScriptEnabled")
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- contentWebView = (WebView) findViewById(R.id.webview);
-
- contentWebView.getSettings().setJavaScriptEnabled(true);
-
- contentWebView.loadUrl("http://www.weim.me/12408.html");
-
- contentWebView.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");
- contentWebView.setWebViewClient(new MyWebViewClient());
-
- }
-
-
- private void addImageClickListner() {
-
- 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); " +
- " } " +
- "}" +
- "})()");
- }
-
-
- public class JavascriptInterface {
-
- private Context context;
-
- public JavascriptInterface(Context context) {
- this.context = context;
- }
-
- public void 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);
- }
- }
-
-
- private class MyWebViewClient extends WebViewClient {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
-
- return super.shouldOverrideUrlLoading(view, url);
- }
-
- @Override
- public void onPageFinished(WebView view, String url) {
-
- view.getSettings().setJavaScriptEnabled(true);
-
- super.onPageFinished(view, url);
-
- addImageClickListner();
-
- }
-
- @Override
- public void onPageStarted(WebView view, String url, Bitmap favicon) {
- view.getSettings().setJavaScriptEnabled(true);
-
- super.onPageStarted(view, url, favicon);
- }
-
- @Override
- public void 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;
-
- public class ShowWebImageActivity extends Activity {
- private TextView imageTextView = null;
- private String imagePath = null;
- private ZoomableImageView imageView = null;
-
- @Override
- protected void 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();
- }
- }
-
- public static Drawable loadImageFromUrl(String url) throws IOException {
-
- URL m = new URL(url);
- InputStream i = (InputStream) m.getContent();
- Drawable d = Drawable.createFromStream(i, "src");
- return d;
- }
- }
图片布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
-
-
- <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>
希望对大家有所帮助
源代码附上