人生有限,学习无限。
本讲内容:WebView组件的使用方法
一、WebSettings 类的用法
WebSettings是WebView组件的一个辅助类,它管理WebView的设置状态。该对象可以通过WebView.getSettings()方法获得。下面举几个例子来说明WebSettings的用法:
webView.getSettings().setJavaScriptEnabled(true) | 得到WebSettings对象,设置支持Javascript的参数 |
webView.getSettings().setSupportZoom(true) | 设置可以支持缩放 |
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR) | 设置默认缩放方式尺寸是far |
webView.getSettings().setBuiltInZoomControls(true) | 设置出现缩放工具 |
二、加载assets目录下的本地网页和加载sdcard下的本地网页
加载URL assets目录下的内容可以用 "file:///android_asset" 前缀
webView.loadUrl("file:///android_asset/html/text1.html");
//加载sdcard中的html文件
webView.loadUrl("file:///mnt/sdcard/text1.html");
我们通过一个例子感受一下,代码的讲解都写在注释里了
在assets/html和sdcard下新建一个Http文件,内容略去,(不懂可以看我的Http博客)
下面是res/layout/activity_main.xml 布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.text1.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用LoadUrl方法加载assets目录下的Html文件"/>
<Button
android:id="@+id/b2"
android:layout_below="@id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用LoadUrl方法加载sdcard下的Html文件"/>
<Button
android:id="@+id/b3"
android:layout_below="@id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存当前页面截图到sdcard中"/>
<WebView
android:id="@+id/webView"
android:layout_below="@id/b3"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity implements OnClickListener {
private Button b1;
private Button b2;
private Button b3;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
webView = (WebView) findViewById(R.id.webView);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
// 加载URL assets目录下的内容可以用 "file:///android_asset" 前缀
webView.loadUrl("file:///android_asset/html/text1.html");
break;
case R.id.b2:
//加载sdcard中的html文件
webView.loadUrl("file:///mnt/sdcard/text1.html");
break;
case R.id.b3:
//取得android.graphics.Picture实例
Picture pic = webView.capturePicture();
int width = pic.getWidth();
int height = pic.getHeight();
if (width > 0 && height > 0) {
//创建指定高宽的Bitmap对象
Bitmap bmp = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
//创建Canvas,并以bitmap为绘制目标
Canvas canvas = new Canvas(bmp);
//将WebView影像绘制在Canvas上
pic.draw(canvas);
try {
String fileName = "sdcard/" + System.currentTimeMillis()
+ ".png";
FileOutputStream fos = new FileOutputStream(fileName);
if (fos != null) {
//压缩bitmap到输出流中
bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
}
Toast.makeText(getApplicationContext(),
"截图成功,文件名是:" + fileName, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO: handle exception
}
}
break;
}
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
下面是运行结果:
本讲就到这里,Take your time and enjoy it