本文实例为大家分享了AsyncTask异步类实现网页内容放大缩小的详细代码,供大家参考,具体内容如下
WebActivity.java:
package com.supermario.filemanager;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import org.apache.http.protocol.HTTP;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.webkit.WebSettings;import android.webkit.WebView;import android.widget.RelativeLayout;import android.widget.ZoomControls;public class WebActivity extends Activity { //网页浏览器 private WebView webView; //进度条布局和网页内容主体布局 private RelativeLayout loadingLayout,webLayout; //放大缩小控制器 private ZoomControls zoomControls;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.web); //初始化页面组件 webView = (WebView)findViewById(R.id.webkit); loadingLayout = (RelativeLayout)findViewById(R.id.loadingLayout); webLayout = (RelativeLayout)findViewById(R.id.weblayout); zoomControls = (ZoomControls)findViewById(R.id.zoomControls);WebSettings webSettings = webView.getSettings(); //设置可以使用js脚本 webSettings.setJavaScriptEnabled(true); //执行异步进程 new MyAsyncTask().execute("");} private void reading(){ String filePath = getIntent().getStringExtra("filePath"); if (filePath != null) {//读取文件webView.loadData(readWebDataToStringFromPath(filePath, new FileReadOverBack() {@Overridepublic void fileReadOver() {}}), "text/html", HTTP.UTF_8); } else {new AlertDialog.Builder(WebActivity.this).setTitle("出错了").setMessage("获取文件路径出错!").setPositiveButton("返回", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) { WebActivity.this.finish();}}); } } //将网页数据读取到一个字符串变量中 private String readWebDataToStringFromPath(String path,final FileReadOverBack fileReadOverBack){ File file = new File(path); StringBuffer stringBuffer = new StringBuffer(); try {//读取文件内容FileInputStream inputStream = new FileInputStream(file);byte[] bytes = new byte[1024];int readCount = 0;while ((readCount = inputStream.read(bytes)) > 0) {stringBuffer.append(new String(bytes, 0, readCount));}fileReadOverBack.fileReadOver(); } catch (FileNotFoundException e) {return "文件不存在!"; } catch (IOException e) {return "文件读取错误!"; } return stringBuffer.toString(); }interface FileReadOverBack{ void fileReadOver(); } //异步处理类 class MyAsyncTask extends AsyncTask<String, String, String>{ //首先执行的函数 @Override protected void onPreExecute() {super.onPreExecute();loadingLayout.setVisibility(View.VISIBLE);webLayout.setVisibility(View.GONE); } //后台执行 @Override protected String doInBackground(String... params) {reading();return null; }@Override protected void onPostExecute(String result) {super.onPostExecute(result);//设置载入进度条隐藏loadingLayout.setVisibility(View.GONE);//设置浏览器内容可见webLayout.setVisibility(View.VISIBLE);// 放大按钮zoomControls.setOnZoomInClickListener(new View.OnClickListener() {//将网页内容放大@Overridepublic void onClick(View v) { webView.zoomIn();}});// 缩小按钮zoomControls.setOnZoomOutClickListener(new View.OnClickListener() { //将网页内容缩小@Overridepublic void onClick(View v) { webView.zoomOut();}}); }}}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。