Android WebView
package com.zkc.pinter.activity; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Picture; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.Menu; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.btpdemo76.R; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by NickChung on 3/20/15. */ public class PrintWebActivity extends Activity { List<Map<String, String>> listData = new ArrayList<Map<String, String>>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_print_web); final EditText txtUrl = (EditText) findViewById(R.id.txtURL); final WebView mWebView = (WebView) findViewById(R.id.wvSite); final Button btnGo = (Button) findViewById(R.id.btnGo); final Button btnPrint = (Button) findViewById(R.id.btnPrint); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); txtUrl.setText("http://www.baidu.com/"); mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url); return true; } }); mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub if (newProgress == 100) { Toast.makeText(PrintWebActivity.this, "加载完成", Toast.LENGTH_LONG).show(); } else { if (newProgress <= 10) { Toast.makeText(PrintWebActivity.this, "加载中...", Toast.LENGTH_LONG).show(); } } } }); btnGo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mWebView.loadUrl(txtUrl.getText().toString()); } }); btnPrint.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Picture pic = mWebView.capturePicture(); int width = pic.getWidth(); int height = pic.getHeight(); if (width > 0 && height > 0) { Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); pic.draw(canvas); try { /* String fileName = Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png"; FileOutputStream fos = new FileOutputStream(fileName); if (fos != null) { bmp.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.close(); } Toast.makeText(getApplicationContext(), "截图成功,文件名是:" + fileName, Toast.LENGTH_SHORT).show(); */ PrintActivity.pl.printImage(bmp); } catch (Exception e) { e.printStackTrace(); } } } }); //PrintActivity.pl.printText(mWebView.getTitle() + "\r\n"); //PrintActivity.pl.printImage(BitmapFactory.decodeResource(getResources(), R.drawable.demo)); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。