代码改变世界

WebView,带划屏手势的浏览器实现

2011-05-04 22:17  bitfairyland  阅读(4410)  评论(3编辑  收藏  举报
写了简单Android环境下基于webview的浏览器,实现划屏切换页面的手势,在一个activity里webview的缓冲内切换的。并测试下调用系统拍照的功能
MVC模式(Model-View-Controller)
1.WebView的设置部分
View Code
1 private void showViews() {
2 // TODO Auto-generated method stub
3   mGestureDetector = new GestureDetector(this);//实例化手势对象
4   wv_vm.getSettings().setSupportZoom(true);//启用页面的缩放
5   wv_vm.getSettings().setBuiltInZoomControls(true);//启用页面缩放的按钮
6   wv_vm.getSettings().setJavaScriptEnabled(true);//启用javascript支持
7   wv_vm.loadUrl("http://www.cnblogs.com/pxue/");//加载网址
8  
9 wv_vm.setOnTouchListener(this);//监听触摸事件
10   wv_vm.setClickable(true);
11 wv_vm.setLongClickable(true);
12
13
14 mGestureDetector.setIsLongpressEnabled(true);
15
16
17 wv_vm.setWebViewClient(new HelloWebViewClient());//实现点击加载页面在本webview内载入
18   wv_vm.setFocusable(true);
19 wv_vm.requestFocus();
20
21 }
在WebView加载新开的页面,是重写了android.webkit.WebViewClient
View Code
1 private class HelloWebViewClient extends WebViewClient {
2 @Override
3 public boolean shouldOverrideUrlLoading(WebView view, String url) {
4 view.loadUrl(url);
5 return true;
6 }
7 }
2.划屏手势部分
监听触摸时间传给手势对象
View Code
1 @Override
2 public boolean onTouch(View v, MotionEvent event) {
3 // TODO Auto-generated method stub
4  // Toast.makeText(this, "onTouch", Toast.LENGTH_SHORT).show();
5   return mGestureDetector.onTouchEvent(event);
6 }
重写了划动事件
View Code
1 @Override
2 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
3 float velocityY) {
4 // TODO Auto-generated method stub
5   if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
6 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
7 wv_vm.goBack();
8 } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
9 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
10
11 wv_vm.goForward();
12 }
13 return false;
14 }
变量、常量的声明
View Code
1 private GestureDetector mGestureDetector;
2
3 private static final int SWIPE_MIN_DISTANCE = 120;
4 private static final int SWIPE_THRESHOLD_VELOCITY = 200;
3.调用系统拍照功能部分
View Code
1 private void setListensers() {
2 // TODO Auto-generated method stub
3   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
4 startActivityForResult(intent, 1);
5
6 }
7
8 private void findViews() {
9 // TODO Auto-generated method stub
10   img_pic=(ImageView)findViewById(R.id.img_pic);
11
12 }
13
14 protected void onActivityResult(int requestCode, int resultCode, Intent data)
15 {
16 if (requestCode == 1)
17 {
18 if (resultCode == Activity.RESULT_OK)
19 {
20 // 拍照Activity保存图像数据的key是data,返回的数据类型是Bitmap对象
21   Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
22 // 在ImageView组件中显示拍摄的照片
23   img_pic.setImageBitmap(cameraBitmap);
24 }
25 }
26 super.onActivityResult(requestCode, resultCode, data);
27 }
小demo的源码本来上传到csdn了,可在我上传的资源就是找不到,可能在审核,等我之后不上下载链接吧
想要demo源码也可以邮件给我