Android利用AsyncTask異步類實現(xiàn)網(wǎng)頁內(nèi)容放大縮小
本文實例為大家分享了AsyncTask異步類實現(xiàn)網(wǎng)頁內(nèi)容放大縮小的詳細(xì)代碼,供大家參考,具體內(nèi)容如下
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 {
//網(wǎng)頁瀏覽器
private WebView webView;
//進(jìn)度條布局和網(wǎng)頁內(nèi)容主體布局
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();
//設(shè)置可以使用js腳本
webSettings.setJavaScriptEnabled(true);
//執(zhí)行異步進(jìn)程
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();
}
});
}
}
//將網(wǎng)頁數(shù)據(jù)讀取到一個字符串變量中
private String readWebDataToStringFromPath(String path,final FileReadOverBack fileReadOverBack){
File file = new File(path);
StringBuffer stringBuffer = new StringBuffer();
try {
//讀取文件內(nèi)容
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>{
//首先執(zhí)行的函數(shù)
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingLayout.setVisibility(View.VISIBLE);
webLayout.setVisibility(View.GONE);
}
//后臺執(zhí)行
@Override
protected String doInBackground(String... params) {
reading();
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//設(shè)置載入進(jìn)度條隱藏
loadingLayout.setVisibility(View.GONE);
//設(shè)置瀏覽器內(nèi)容可見
webLayout.setVisibility(View.VISIBLE);
// 放大按鈕
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
//將網(wǎng)頁內(nèi)容放大
@Override
public void onClick(View v) {
webView.zoomIn();
}
});
// 縮小按鈕
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
//將網(wǎng)頁內(nèi)容縮小
@Override
public void onClick(View v) {
webView.zoomOut();
}
});
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Android中AsyncTask的使用方法
- Android 中糟糕的AsyncTask
- Android中通過AsyncTask類來制作炫酷進(jìn)度條的實例教程
- 詳解Android App中的AsyncTask異步任務(wù)執(zhí)行方式
- Android使用AsyncTask實現(xiàn)多線程下載的方法
- Android中AsyncTask異步任務(wù)使用詳細(xì)實例(一)
- Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片
- 詳解Android中AsyncTask機制
- Android通過Handler與AsyncTask兩種方式動態(tài)更新ListView(附源碼)
- Android中AsyncTask與handler用法實例分析
- Android AsyncTask 后監(jiān)聽異步加載完畢的動作詳解
相關(guān)文章
Android自定義PasswordInputView密碼輸入
這篇文章主要為大家詳細(xì)介紹了Android自定義PasswordInputView密碼輸入功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android自定義有限制區(qū)域的圖例角度自識別涂鴉工具類完結(jié)篇
這篇文章主要為大家介紹了Android自定義有限制區(qū)域的圖例角度自識別涂鴉工具類完結(jié)篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法
這篇文章主要介紹了Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法,涉及Android針對桌面快捷方式的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05
Android中findViewById獲取控件返回為空問題怎么解決
這篇文章主要介紹了Android中findViewById獲取控件返回為空問題怎么解決的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06
Android EditText限制輸入整數(shù)和小數(shù)的位數(shù)的方法示例
這篇文章主要介紹了Android EditText限制輸入整數(shù)和小數(shù)的位數(shù)的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

