Android 中的移動感知功能
Android簡介使用方法開發工具開始寫程式視覺化介面視覺元件對話元件核心物件事件處理資料儲存查詢功能影音功能繪圖功能網路功能衛星地圖特殊功能資源管理裝置管理系統核心問題與回答刷機升級常用軟體Eclipse教學錄影訊息相關網站參考文獻最新修改簡體版English |
專案下載:Sensor1.zip 執行結果本範例必須在手機上執行,每 0.5 秒會顯示一次 (TYPE_ACCELEROMETER) ax, ay, ax 與 (TYPE_ORIENTATION) ox, oy, oz。在模擬器上由於沒有移動感知器,因此會顯示下列圖形。 其中的 ox, oy, oz 的意義如下
values[1]: Pitch, rotation around X axis (-180 to 180), with positive values when the z-axis moves toward the y-axis. 程式範例:Sensor1.javapackage ccc.android; import java.util.Date; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.View; public class Sensor1 extends Activity { SensorObj mSensor = null; Panel mPanel = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSensor = new SensorObj() { long time = new Date().getTime(); public void onSensorChanged(SensorEvent event) { long now = new Date().getTime(); if (now - time > 500) synchronized (this) { setData(event); mPanel.invalidate(); time = now; } } }; mPanel = new Panel(Sensor1.this); setContentView(mPanel); } @Override protected void onResume() { super.onResume(); mSensor.register(); } @Override protected void onStop() { super.onStop(); mSensor.unregister(); } class Panel extends View { public Panel(Context context) { super(context); } public void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.WHITE); SensorObj s = Sensor1.this.mSensor; canvas.drawText("ax:"+s.ax, 1, 20, paint); canvas.drawText("ay:"+s.ay, 1, 40, paint); canvas.drawText("az:"+s.az, 1, 60, paint); canvas.drawText("ox:"+s.ox, 1, 80, paint); canvas.drawText("oy:"+s.oy, 1, 100, paint); canvas.drawText("oz:"+s.oz, 1, 120, paint); } } class SensorObj implements SensorEventListener { SensorManager sensorManager = null; float ax, ay, az; float ox, oy, oz; public SensorObj() { sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); } public void setData(SensorEvent event) { switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: this.ax = event.values[0]; this.ay = event.values[1]; this.az = event.values[2]; break; case Sensor.TYPE_ORIENTATION: this.ox = event.values[0]; this.oy = event.values[1]; this.oz = event.values[2]; break; } } public void register() { sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME); } public void unregister() { sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)); sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION)); } public void onSensorChanged(SensorEvent event) {} public void onAccuracyChanged(Sensor sensor, int accuracy) {} } } 參考文獻
|
page revision: 5, last edited: 22 Dec 2011 00:20
Post preview:
Close preview