理解:点击图片浏览的主要原理就是通过webview为网页中的html元素添加onclick事件,弹出activity
package com.topnews;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.topnews.base.BaseActivity;
import com.topnews.bean.NewsEntity;
import com.topnews.service.NewsDetailsService;
import com.topnews.tool.DateTools;
import java.util.ArrayList;
@SuppressLint("JavascriptInterface")
public class DetailsActivity extends BaseActivity {
private TextView title;
private ProgressBar progressBar;
private FrameLayout customview_layout;
private String news_url;
private String news_title;
private String news_source;
private String news_date;
private NewsEntity news;
private TextView action_comment_count;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
setNeedBackGesture(true);//
getData();
initView();
initWebView();
}
private void getData() {
news = (NewsEntity) getIntent().getSerializableExtra("news");
news_url = news.getSource_url();
news_title = news.getTitle();
news_source = news.getSource();
news_date = DateTools.getNewsDetailsDate(String.valueOf(news.getPublishTime()));
}
private void initWebView() {
webView = (WebView)findViewById(R.id.wb_details);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
if (!TextUtils.isEmpty(news_url)) {
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
// settings.setTextZoom(120);//Sets the text zoom of the page in percent. The default is 100.
settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
settings.setAppCacheEnabled(true);
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
// settings.setUseWideViewPort(true);
// settings.setLoadWithOverviewMode(true);
settings.setBuiltInZoomControls(false);
webView.setBackgroundResource(R.color.transparent);
webView.addJavascriptInterface(new JavascriptInterface(getApplicationContext()),"imagelistner");
webView.setWebChromeClient(new MyWebChromeClient());
webView.setWebViewClient(new MyWebViewClient());
new MyAsnycTask().execute(news_url, news_title, news_source + " " +news_date);
}
}
private void initView() {
title = (TextView) findViewById(R.id.title);
progressBar = (ProgressBar) findViewById(R.id.ss_htmlprogessbar);
customview_layout = (FrameLayout) findViewById(R.id.customview_layout);
action_comment_count = (TextView) findViewById(R.id.action_comment_count);
progressBar.setVisibility(View.VISIBLE);
title.setTextSize(13);
title.setVisibility(View.VISIBLE);
title.setText(news_url);
action_comment_count.setText(String.valueOf(news.getCommentNum()));
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
private class MyAsnycTask extends AsyncTask<String, String,String>{
@Override
protected String doInBackground(String... urls) {
String data=NewsDetailsService.getNewsDetails(urls[0],urls[1],urls[2]);
return data;
}
@Override
protected void onPostExecute(String data) {
webView.loadDataWithBaseURL (null, data, "text/html", "utf-8",null);
}
}
//webView 通过loadUrl方法执行一段javascript方法,为网页中的元素添加javascript方法
private void addImageClickListner() {
webView.loadUrl("javascript:(function(){"
+ "var objs = document.getElementsByTagName(\"img\");"
+ "var imgurl=''; " + "for(var i=0;i<objs.length;i++) " + "{"
+ "imgurl+=objs[i].src+',';"
+ " objs[i].οnclick=function() " + " { "
+ " window.imagelistner.openImage(imgurl); "
+ " } " + "}" + "})()");
}
//这是一个自定义的类,webView.addJavascriptInterface(new JavascriptInterface(getApplicationContext()),"imagelistner");方法把我们写的方法和
//webView结合在一起,这样在javascript中就可以通过window.imagelistner.openImage(imgurl); "调用了
private Context context;
public JavascriptInterface(Context context) {
this.context = context;
}
public void openImage(String img) {
//
String[] imgs = img.split(",");
ArrayList<String> imgsUrl = new ArrayList<String>();
for (String s : imgs) {
imgsUrl.add(s);
}
Intent intent = new Intent();
intent.putStringArrayListExtra("infos", imgsUrl);
intent.setClass(context, ImageShowActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
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();
progressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
@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) {
progressBar.setVisibility(View.GONE);
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
private class MyWebChromeClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
if(newProgress != 100){
progressBar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
}
}