寫了簡單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/" );//載入網址 |
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()); //實現點擊載入頁面在本webview內載入 |
16 |
wv_vm.setFocusable( true ); |
在WebView載入新開的頁面,是重寫了Android.webkit.WebViewClient
查看原始碼列印協助
1 |
private class HelloWebViewClient extends
WebViewClient { |
3 |
public boolean shouldOverrideUrlLoading(WebView view, String url) { |
2.劃屏手勢部分監聽觸摸時間傳給手勢對象查看原始碼列印協助
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);
|
重寫了划動事件
查看原始碼列印協助
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() > SWIPE_MIN_DISTANCE
|
06 |
&& Math.abs(<span class = "wp_keywordlink_affiliate" ><a href= "http://www.mikel.cn/tag/velocity/"
title= "Velocity"
target= "_blank" >Velocity</a></span>X) > SWIPE_THRESHOLD_VELOCITY) { |
08 |
} else
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE |
09 |
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { |
變數、常量的聲明
查看原始碼列印協助
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() { |
02 |
// TODO Auto-generated method stub |
03 |
Intent intent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
04 |
startActivityForResult(intent,
1 ); |
08 |
private void findViews() { |
09 |
// TODO Auto-generated method stub |
10 |
img_pic=(ImageView)findViewById(R.id.img_pic); |
14 |
protected void onActivityResult( int
requestCode, int
resultCode, Intent data) |
18 |
if (resultCode == Activity.RESULT_OK)
|
20 |
// 拍照Activity儲存映像資料的key是data,返回的資料類型是Bitmap對象 |
21 |
Bitmap cameraBitmap = (Bitmap) data.getExtras().get( "data" ); |
22 |
// 在ImageView組件中顯示拍攝的照片 |
23 |
img_pic.setImageBitmap(cameraBitmap); |
26 |
super .onActivityResult(requestCode, resultCode, data); |