WebView相当于手机一个嵌入式的浏览器,可以加载并显示网页,使用WebKit渲染引擎。使用WebView的步骤:
1. 布局文件中添加WebView组件
2. 在Activity中实例化该组件
3. 设置WebView客户端,如果不设置,则使用默认浏览器
4. 加载URL,显示网页
注意:需要在注册文件中添加相关权限:
<uses-permission android:name="android.permission.INTERNET"/>
实现浏览器
public class MainActivity extends Activity {
EditText et;
Button btn;
WebView wv;
WebSettings settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
wv=(WebView)findViewById(R.id.webView1);
//得到webView设置
settings=wv.getSettings();
//能显示JavaScript
settings.setJavaScriptEnabled(true);
// AppCacheMaxSize in 512 KB = 524288 bytes
// settings.setAppCacheMaxSize(524288);
// Tell the WebView to enable Application Caches
// settings.setAppCacheEnabled(true);
// 延含学习setAppCachePath()指定缓存路径
//mWebSettings01.setDatabaseEnabled(true);
//mWebSettings01.setDatabasePath("/data/data/com.package.name/databases");
//新建客户端
WebViewClient client=new WebViewClient(){
//重写3个方法
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO 自动生成的方法存根
Toast.makeText(getBaseContext(), "正在载入...", Toast.LENGTH_SHORT).show();
super.onPageStarted(view, url, favicon);
}
@Override
//载入完成截屏保存
public void onPageFinished(WebView view, String url) {
// TODO 自动生成的方法存根
//得到网页的图片
@SuppressWarnings("deprecation")
Picture picture=view.capturePicture();
//得到图片的宽和高
int width=picture.getWidth();
int height=picture.getHeight();
//如果宽或高为0,不执行操作
if(width*height==0)
return;
//新建位图用于存数据
Bitmap bitmap=Bitmap.createBitmap(width, height, Config.ARGB_8888);
//新建画布,指定要画的位图对象
Canvas canvas=new Canvas(bitmap);
//将图片画到画布上
picture.draw(canvas);
try{
File file=new File("/sdcard/pic.jpg");
if(file.exists())
file.delete();
file.createNewFile();
FileOutputStream fos=new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
super.onPageFinished(view, url);
}
@Override
public void onLoadResource(WebView view, String url) {
// TODO 自动生成的方法存根
super.onLoadResource(view, url);
}
};
//设置客户端,如果没有设置,则调用内置的浏览器
wv.setWebViewClient(client);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
String url=et.getText().toString();
if(URLUtil.isNetworkUrl(url)){
//加载url
wv.loadUrl(url);
}
else
{
Toast.makeText(getBaseContext(), "非法网址", Toast.LENGTH_SHORT).show();
}
}
});
}
//设置菜单栏,实现浏览器页面的前进后退
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("deprecation")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.go:
{
if(wv.canGoForward())
wv.goForward();
}
break;
case R.id.back:
{
if(wv.canGoBack())
wv.goBack();
}
break;
case R.id.zoomOut:
{
// settings.setBuiltInZoomControls(true); // 显示放大缩小 controler
// settings.setSupportZoom(true); // 可以缩放
//settings.setDefaultZoom(ZoomDensity.CLOSE);// 默认缩放模式
// settings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
}break;
case R.id.zoomIn:
// settings.setDefaultZoom(WebSettings.ZoomDensity.FAR);break;
case R.id.Default:
// settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}