Nonsense not much to say, directly on the code:
Private WebView Mwebview;
Mwebview.getsettings (). Setjavascriptenabled (true);//This does not say, first of all have to support JS
Mwebview.loadurl ("Www.111cn.net");
Mwebview.addjavascriptinterface (The new Myjavascriptinterface (this), "Imagelistner");//Customize a Myjavascript interface
Mwebview.setwebviewclient (New Mywebviewclient ());//mywebviewclient inherited from Webviewclient
Myjavascriptinterface:
JS Communication interface
public class Myjavascriptinterface {
private context;
Public Myjavascriptinterface {
This.context = context;
}
@JavascriptInterface//It is worth noting that when your openimage method is not executed, that means Mwebview.addjavascriptinterface does not work
Then please add @javascriptinterface here, plus remember to import first
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);
After HTML loading is complete, add the Click JS function of the listener image
Addimageclicklistner ()//This method is below
}
@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);
}
}
Inject JS function to monitor
private void Addimageclicklistner () {
The function of this JS function is to iterate through all the IMG points, add the onclick function, and call the local interface pass URL in the past when executing.
Mwebview.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); " +
" } " +
"}" +
"})()");
}
class that shows pictures
Package cn.zmit.xiangrikui.test;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.Matrix;
Import android.graphics.drawable.Drawable;
Import Android.os.Bundle;
Import Android.view.Display;
Import Android.view.View;
Import Android.widget.ImageView;
Import Com.squareup.picasso.Picasso;
Import CN.ZMIT.XIANGRIKUI.R;
public class Showwebimageactivity extends activity {
Private String ImagePath = null;
Private ImageView ImageView = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.show_webimage);
ImagePath = Getintent (). Getstringextra ("image");
ImageView = (ImageView) Findviewbyid (R.id.show_webimage_imageview);
Imageview.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Finish ();
}
});
Com.squareup.picasso.Target Target = new Com.squareup.picasso.Target () {
@Override
public void onbitmaploaded (Bitmap Bitmap, picasso.loadedfrom from) {
Imageview.setimagebitmap (Big (bitmap));
}
@Override
public void onbitmapfailed (drawable errordrawable) {
}
@Override
public void Onprepareload (drawable placeholderdrawable) {
}
};
Picasso.with (this). Load (ImagePath). into (target);
}
Public Bitmap Big (Bitmap b) {
Display d = (showwebimageactivity.this). Getwindowmanager (). Getdefaultdisplay ();
int x = D.getwidth ();//Current screen width px
int y = d.getheight ();//current screen height px
int w = b.getwidth ();
int h = b.getheight ();
float SX = (float) x/w;//conversion, do not convert mine is always dead in this.
Float sy = (float) y/h;
Float total=math.min (sx,sy);
Matrix matrix = new Matrix ();
Matrix.postscale (total, total); Ratio of long and wide magnification reduction
Bitmap resizebmp = bitmap.createbitmap (b, 0, 0, W,
h, Matrix, true);
return resizebmp;
}
}