欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android利用AsyncTask异步类实现网页内容放大缩小

程序员文章站 2024-03-04 21:35:00
本文实例为大家分享了asynctask异步类实现网页内容放大缩小的详细代码,供大家参考,具体内容如下 webactivity.java: package c...

本文实例为大家分享了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() {
  @override
  public void filereadover() {
  }
  }), "text/html", http.utf_8);
 } else {
  new alertdialog.builder(webactivity.this).settitle("出错了").setmessage("获取文件路径出错!").setpositivebutton("返回", new onclicklistener() {
  @override
  public 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() {
  //将网页内容放大
  @override
  public void onclick(view v) {
   webview.zoomin();
  }
  });
  // 缩小按钮
  zoomcontrols.setonzoomoutclicklistener(new view.onclicklistener() { 
  //将网页内容缩小
  @override
  public void onclick(view v) {
   webview.zoomout();
  }
  });
 } 
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。