Android利用AsyncTask异步类实现网页内容放大缩小
本文实例为大家分享了AsyncTask异步类实现网页内容放大缩小的详细代码,供大家参考,具体内容如下
WebActivity.java:
packagecom.supermario.filemanager;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importorg.apache.http.protocol.HTTP;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.DialogInterface;
importandroid.content.DialogInterface.OnClickListener;
importandroid.os.AsyncTask;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.webkit.WebSettings;
importandroid.webkit.WebView;
importandroid.widget.RelativeLayout;
importandroid.widget.ZoomControls;
publicclassWebActivityextendsActivity{
//网页浏览器
privateWebViewwebView;
//进度条布局和网页内容主体布局
privateRelativeLayoutloadingLayout,webLayout;
//放大缩小控制器
privateZoomControlszoomControls;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
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);
WebSettingswebSettings=webView.getSettings();
//设置可以使用js脚本
webSettings.setJavaScriptEnabled(true);
//执行异步进程
newMyAsyncTask().execute("");
}
privatevoidreading(){
StringfilePath=getIntent().getStringExtra("filePath");
if(filePath!=null){
//读取文件
webView.loadData(readWebDataToStringFromPath(filePath,newFileReadOverBack(){
@Override
publicvoidfileReadOver(){
}
}),"text/html",HTTP.UTF_8);
}else{
newAlertDialog.Builder(WebActivity.this).setTitle("出错了").setMessage("获取文件路径出错!").setPositiveButton("返回",newOnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,intwhich){
WebActivity.this.finish();
}
});
}
}
//将网页数据读取到一个字符串变量中
privateStringreadWebDataToStringFromPath(Stringpath,finalFileReadOverBackfileReadOverBack){
Filefile=newFile(path);
StringBufferstringBuffer=newStringBuffer();
try{
//读取文件内容
FileInputStreaminputStream=newFileInputStream(file);
byte[]bytes=newbyte[1024];
intreadCount=0;
while((readCount=inputStream.read(bytes))>0){
stringBuffer.append(newString(bytes,0,readCount));
}
fileReadOverBack.fileReadOver();
}catch(FileNotFoundExceptione){
return"文件不存在!";
}catch(IOExceptione){
return"文件读取错误!";
}
returnstringBuffer.toString();
}
interfaceFileReadOverBack{
voidfileReadOver();
}
//异步处理类
classMyAsyncTaskextendsAsyncTask<String,String,String>{
//首先执行的函数
@Override
protectedvoidonPreExecute(){
super.onPreExecute();
loadingLayout.setVisibility(View.VISIBLE);
webLayout.setVisibility(View.GONE);
}
//后台执行
@Override
protectedStringdoInBackground(String...params){
reading();
returnnull;
}
@Override
protectedvoidonPostExecute(Stringresult){
super.onPostExecute(result);
//设置载入进度条隐藏
loadingLayout.setVisibility(View.GONE);
//设置浏览器内容可见
webLayout.setVisibility(View.VISIBLE);
//放大按钮
zoomControls.setOnZoomInClickListener(newView.OnClickListener(){
//将网页内容放大
@Override
publicvoidonClick(Viewv){
webView.zoomIn();
}
});
//缩小按钮
zoomControls.setOnZoomOutClickListener(newView.OnClickListener(){
//将网页内容缩小
@Override
publicvoidonClick(Viewv){
webView.zoomOut();
}
});
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。