Android:WebView带划屏手势的浏览器实现
转载
写了简单Android环境下基于webview的浏览器,实现划屏切换页面的手势,在一个activity里webview的缓冲内切换的。并测试下调用系统拍照的功能
MVC模式(Model-View-Controller)
1.WebView的设置部分
01 | private void showViews() { |
03 | mGestureDetector = new GestureDetector( this ); |
04 | wv_vm.getSettings().setSupportZoom( true ); |
05 | wv_vm.getSettings().setBuiltInZoomControls( true ); |
06 | wv_vm.getSettings().set<SPAN class =wp_keywordlink_affiliate><A title=JavaScript href= "http://www.mikel.cn/category/%e5%bc%80%e5%8f%91%e7%ac%94%e8%ae%b0/javascript" target=_blank>JavaScript</A></SPAN>Enabled( true );//启用<SPAN class =wp_keywordlink_affiliate><A title=JavaScript href= "http://www.mikel.cn/category/%e5%bc%80%e5%8f%91%e7%ac%94%e8%ae%b0/javascript" target=_blank>JavaScript</A></SPAN>支持 |
07 | wv_vm.loadUrl( "javascript:void(0)" );//加载网址 |
09 | wv_vm.setOnTouchListener( this ); |
10 | wv_vm.setClickable( true ); |
11 | wv_vm.setLongClickable( true ); |
13 | mGestureDetector.setIsLongpressEnabled( true ); |
15 | wv_vm.setWebViewClient( new HelloWebViewClient()); |
16 | wv_vm.setFocusable( true ); |
17 | wv_vm.requestFocus(); |
在WebView加载新开的页面,是重写了Android.webkit.WebViewClient
1 | private class HelloWebViewClient extends WebViewClient { |
3 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
4 | view.loadUrl(url); |
5 | return true ; |
2.划屏手势部分
监听触摸时间传给手势对象
2 | public boolean onTouch(View v, MotionEvent event) { |
5 | return mGestureDetector.onTouchEvent(event); |
重写了划动事件
02 | public boolean onFling(MotionEvent e1, MotionEvent e2, float <SPAN class =wp_keywordlink_affiliate><A title=Velocity href= "http://www.mikel.cn/tag/velocity/" target=_blank>Velocity</A></SPAN>X, |
03 | float <SPAN class =wp_keywordlink_affiliate><A title=Velocity href= "http://www.mikel.cn/tag/velocity/" target=_blank>Velocity</A></SPAN>Y) { |
05 | if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE |
06 | && Math.abs(<SPAN class =wp_keywordlink_affiliate><A title=Velocity href= "http://www.mikel.cn/tag/velocity/" target=_blank>Velocity</A></SPAN>X) > SWIPE_THRESHOLD_VELOCITY) { |
07 | wv_vm.goBack(); |
08 | } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE |
09 | && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { |
11 | wv_vm.goForward(); |
13 | return false ; |
变量、常量的声明
1 | private GestureDetector mGestureDetector; |
3 | private static final int SWIPE_MIN_DISTANCE = 120 ; |
4 | private static final int SWIPE_THRESHOLD_VELOCITY = 200 ; |
3.调用系统拍照功能部分
01 | private void setListensers() { |
03 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
04 | startActivityForResult(intent, 1 ); |
08 | private void findViews() { |
10 | img_pic=(ImageView)findViewById(R.id.img_pic); |
14 | protected void onActivityResult( int requestCode, int resultCode, Intent data) |
16 | if (requestCode == 1 ) |
18 | if (resultCode == Activity.RESULT_OK) |
21 | Bitmap cameraBitmap = (Bitmap) data.getExtras().get( "data" ); |
23 | img_pic.setImageBitmap(cameraBitmap); |
26 | super .onActivityResult(requestCode, resultCode, data); |
小demo的源码本来上传到csdn了,可在我上传的资源就是找不到,可能在审核,等我之后不上下载链接吧
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。