国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android利用AsyncTask異步類實現網頁內容放大縮小

2019-12-12 05:55:00
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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();  }  }); }  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂尔多斯市| 瑞昌市| 华宁县| 新巴尔虎左旗| 农安县| 中阳县| 米林县| 临朐县| 晋城| 金昌市| 清水河县| 萨迦县| 贵南县| 道孚县| 报价| 青阳县| 张北县| 梁河县| 马关县| 当阳市| 灵台县| 炎陵县| 土默特右旗| 清流县| 石门县| 昌吉市| 永川市| 宕昌县| 松桃| 东乡| 碌曲县| 南阳市| 大邑县| 德兴市| 渝北区| 乐东| 虎林市| 乐山市| 泸水县| 大宁县| 怀远县|