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