Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android browser 的幾個小feature (二) 左右晃動控制網頁前進後退

android browser 的幾個小feature (二) 左右晃動控制網頁前進後退

編輯:關於Android編程

#############################################

本文為極度寒冰原創,轉載請注明出處 #############################################
這個靈感是來源於現在的各種搖一搖,我們也借助這種新式的玩法,來進行左右晃動來控制網頁的前進後退。
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 00aac08..fff0c50 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -97,6 +97,7 @@ import com.android.browser.provider.BrowserProvider2.Thumbnails;
 import com.android.browser.provider.SnapshotProvider.Snapshots;
 import com.android.browser.mynavigation.AddMyNavigationPage;
 import com.android.browser.mynavigation.MyNavigationUtil;
+import com.android.browser.ShakeDetector.OnShakeListener;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -113,6 +114,13 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
+import android.os.Bundle;
+import android.app.Activity;
+import android.util.Log;
+import android.view.Menu;
+import android.widget.TextView;
+
+
 /**
  * Controller for browser
  */
@@ -239,7 +247,8 @@ public class Controller
     private CrashRecoveryHandler mCrashRecoveryHandler;
 
     private boolean mBlockEvents;
-
+    private ShakeDetector mDetector;
+    private OnShakeListener mListener = null;
     private String mVoiceResult;
     private boolean mUpdateMyNavThumbnail;
     private String mUpdateMyNavThumbnailUrl;
@@ -276,7 +285,7 @@ public class Controller
         mSystemAllowGeolocationOrigins =
                 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
         mSystemAllowGeolocationOrigins.start();
-
+        mDetector = new ShakeDetector(mActivity.getApplicationContext());
         openIconDatabase();
     }
 
@@ -705,6 +714,9 @@ public class Controller
             sThumbnailBitmap.recycle();
             sThumbnailBitmap = null;
         }
+
+        mDetector.unregisterOnShakeListener(mListener);
+        mDetector.stop();
     }
 
     @Override
@@ -755,6 +767,21 @@ public class Controller
             mUi.onVoiceResult(mVoiceResult);
             mVoiceResult = null;
         }
+        mDetector.start();
+        mListener = new OnShakeListener() {
+            @Override
+            public void onShake(int direction) {
+                // TODO Auto-generated method stub
+                if(direction == -1) {
+                    Log.e("chao","Direction = go Forward");
+                    getCurrentTab().goForward();
+                }else {
+                    Log.e("chao","Direction = go back");
+                    getCurrentTab().goBack();
+                }             
+            }
+        };
+        mDetector.registerOnShakeListener(mListener); 
     }
 
     private void releaseWakeLock() {
diff --git a/src/com/android/browser/ShakeDetector.java b/src/com/android/browser/ShakeDetector.java
new file mode 100644
index 0000000..fbafa2e
--- /dev/null
+++ b/src/com/android/browser/ShakeDetector.java
@@ -0,0 +1,101 @@
+package com.android.browser;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import android.content.Context;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+import android.util.FloatMath;
+import android.util.Log;
+/**
+ * ó?óú?ì2aê??úò???
+ */
+public class ShakeDetector implements SensorEventListener {
+	static final int UPDATE_INTERVAL = 10;//original is 100,which will gather less data
+	long mLastUpdateTime;
+	float mLastX, mLastY, mLastZ;
+    final float alpha =  0.8f;
+    float gravity[] = new float[3];
+	Context mContext;
+	SensorManager mSensorManager;
+	ArrayList mListeners;
+	ArrayList list = new ArrayList();
+	public int shakeThreshold = 5000;
+	public ShakeDetector(Context context) {
+		mContext = context;
+		mSensorManager = (SensorManager) context
+				.getSystemService(Context.SENSOR_SERVICE);
+		mListeners = new ArrayList();
+	}
+	public interface OnShakeListener {
+		void onShake(int direction);
+	}
+	public void registerOnShakeListener(OnShakeListener listener) {
+		if (mListeners.contains(listener))
+			return;
+		mListeners.add(listener);
+	}
+	public void unregisterOnShakeListener(OnShakeListener listener) {
+		mListeners.remove(listener);
+	}
+	public void start() {
+		if (mSensorManager == null) {
+			throw new UnsupportedOperationException();
+		}
+		Sensor sensor = mSensorManager
+				.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
+		if (sensor == null) {
+			throw new UnsupportedOperationException();
+		}
+		boolean success = mSensorManager.registerListener(this, sensor,
+				SensorManager.SENSOR_DELAY_GAME);
+		if (!success) {
+			throw new UnsupportedOperationException();
+		}
+	}
+	public void stop() {
+		if (mSensorManager != null)
+			mSensorManager.unregisterListener(this);
+	}
+	@Override
+	public void onAccuracyChanged(Sensor sensor, int accuracy) {
+		// TODO Auto-generated method stub
+	}
+	@Override
+	public void onSensorChanged(SensorEvent event) {
+		long currentTime = System.currentTimeMillis();
+		long diffTime = currentTime - mLastUpdateTime;
+		if (diffTime < UPDATE_INTERVAL)
+			return;
+		mLastUpdateTime = currentTime;
+		float x = event.values[0];
+		float y = event.values[1];
+		float z = event.values[2];
+        gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
+        gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
+        gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
+		float deltaX = x - gravity[0];
+		float deltaY = y - gravity[1];
+		float deltaZ = z - gravity[2];
+		float delta = FloatMath.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ
+				* deltaZ) / diffTime * 10000;
+		if(Math.abs(deltaX) > 8 /*|| Math.abs(deltaY) > 8 || Math.abs(deltaZ) > 8*/) {//deltaZ is 7.8 when hold steady
+			Log.d("chao","deltaX = " + deltaX + ", deltaY = " + deltaY + ", deltaZ = " + deltaZ);
+			list.add(deltaX);
+			if(deltaX > 8) {
+				this.notifyListeners(-1);
+			}else if (deltaX < -8){
+				this.notifyListeners(1);
+			}
+		}
+	}
+	private void notifyListeners(int direction) {
+		for (OnShakeListener listener : mListeners) {
+			listener.onShake(direction);
+		}
+	}
+}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved