Android:WebView帶劃屏手勢的瀏覽器實現

來源:互聯網
上載者:User
寫了簡單Android環境下基於webview的瀏覽器,實現劃屏切換頁面的手勢,在一個activity裡webview的緩衝內切換的。並測試下調用系統拍照的功能MVC模式(Model-View-Controller)1.WebView的設定部分查看原始碼列印協助
01 private
void
showViews() {
02 // TODO Auto-generated method stub
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 href="http://www.mikel.cn/category/%e5%bc%80%e5%8f%91%e7%ac%94%e8%ae%b0/javascript"
title="JavaScript"
target="_blank">JavaScript</a></span>Enabled(true);//啟用<span
class="wp_keywordlink_affiliate"><a href="http://www.mikel.cn/category/%e5%bc%80%e5%8f%91%e7%ac%94%e8%ae%b0/javascript"
title="JavaScript"
target="_blank">JavaScript</a></span>支援
07 wv_vm.loadUrl("http://www.cnblogs.com/pxue/");//載入網址
08  
09 wv_vm.setOnTouchListener(this);//監聽觸摸事件
10 wv_vm.setClickable(true);
11 wv_vm.setLongClickable(true);
12  
13 mGestureDetector.setIsLongpressEnabled(true);
14  
15 wv_vm.setWebViewClient(new
HelloWebViewClient());//實現點擊載入頁面在本webview內載入
16 wv_vm.setFocusable(true);
17 wv_vm.requestFocus();
18  
19 }

在WebView載入新開的頁面,是重寫了Android.webkit.WebViewClient

查看原始碼列印協助
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.劃屏手勢部分監聽觸摸時間傳給手勢對象查看原始碼列印協助
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 }

重寫了划動事件

查看原始碼列印協助
01 @Override
02 public
boolean
onFling(MotionEvent e1, MotionEvent e2,
float <span class="wp_keywordlink_affiliate"><a href="http://www.mikel.cn/tag/velocity/"
title="Velocity"
target="_blank">Velocity</a></span>X,
03 float
<span
class="wp_keywordlink_affiliate"><a href="http://www.mikel.cn/tag/velocity/"
title="Velocity"
target="_blank">Velocity</a></span>Y) {
04 // TODO Auto-generated method stub
05 if
(e1.getX() - e2.getX() &gt; SWIPE_MIN_DISTANCE
06 &amp;&amp; Math.abs(<span class="wp_keywordlink_affiliate"><a href="http://www.mikel.cn/tag/velocity/"
title="Velocity"
target="_blank">Velocity</a></span>X) &gt; SWIPE_THRESHOLD_VELOCITY) {
07 wv_vm.goBack();
08 } else
if (e2.getX() - e1.getX() &gt; SWIPE_MIN_DISTANCE
09 &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) {
10  
11 wv_vm.goForward();
12 }
13 return
false
;
14 }

變數、常量的聲明

查看原始碼列印協助
1 private
GestureDetector mGestureDetector;
2  
3 private
static
final
int
SWIPE_MIN_DISTANCE = 120;
4 private
static
final
int
SWIPE_THRESHOLD_VELOCITY = 200;

3.調用系統拍照功能部分

查看原始碼列印協助
01 private
void
setListensers() {
02 // TODO Auto-generated method stub
03 Intent intent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
04 startActivityForResult(intent,
1);
05  
06 }
07  
08 private
void
findViews() {
09 // 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 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.