Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實作測力計:用 Sensor 感應器在線程中刷新 UI

編輯:關於 Android 程式設計

本文以實例講解 Android 如何運用 Sensor 感應器,在子線程中刷新 UI,進而打造一個類似測力計的應用。以下內容供大家參考。

在前一篇《Android 基於 Sensor 感應器獲取重力感應加速度的方法》中,我們介紹了 Sensor 的基礎知識,以及一個透過加速度感應器取得數據的範例。

前面提過一個問題:感應器的刷新頻率非常快。如果我們要在 UI 中依據數據繪製一個不斷移動的箭頭,那就得過於頻繁地刷新畫面,既佔用大量資源,體驗也很差。《Android 2 高級編程》中有一個演示測力器的例子,無意間提供了一種在這種情況下刷新 UI 的優雅方案,正好解決了「感應器在介面中過於頻繁刷新」的困擾。

以下是我修改後的程式碼,供大家參考:

 /* * @author octobershiner * 2011 07 27 * SE.HIT * 這是《Android 2 高級編程》中的一個實例,關於感應器的使用很普通,但是介紹了一種使用感應器的應用如何刷新UI的好辦法,值得學習 * 我添加了一些注釋和onPause方法 * 一個演示感應器在線程中刷新UI的例子 測力器的應用 * */ package uni.sensor; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.widget.TextView; public class ForceometerActivity extends Activity{ SensorManager sensorManager; TextView accelerationTextView; TextView maxAccelerationTextView; float currentAcceleration = 0; float maxAcceleration = 0; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取兩個文本顯示域 accelerationTextView = (TextView)findViewById(R.id.acceleration); maxAccelerationTextView = (TextView)findViewById(R.id.maxAcceleration); //獲取sensor服務,選擇加速度感應器 sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //注冊事件 sensorManager.registerListener(sensorEventListener, accelerometer, SensorManager.SENSOR_DELAY_FASTEST); Timer updateTimer = new Timer("gForceUpdate"); updateTimer.scheduleAtFixedRate(new TimerTask() { public void run() { updateGUI(); } }, 0, 100); } //添加的新方法,退出activity的時候,關閉監聽器 public void onPause(){ sensorManager.unregisterListener(sensorEventListener); super.onPause(); } private final SensorEventListener sensorEventListener = new SensorEventListener() { //系統設置的重力加速度標准值,設備在水平靜止的情況下就承受這個壓力,所以默認Y軸方向的加速度值為STANDARD_GRAVITY double calibration = SensorManager.STANDARD_GRAVITY; public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { double x = event.values[0]; double y = event.values[1]; double z = event.values[2]; //計算三個方向的加速度 double a = Math.round(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2))); //消去原有的重力引起的壓力 currentAcceleration = Math.abs((float)(a-calibration)); if (currentAcceleration > maxAcceleration) maxAcceleration = currentAcceleration; } }; private void updateGUI() { /* * 推薦的一個刷新UI的方法 * Activity.runOnUiThread(Runnable) * 在新的線程中更新UI * Runnable是一個接口,需要你實現run方法,上面的TimerTask就是實現了這個接口同樣需要實現run方法 * */ runOnUiThread(new Runnable() { public void run() { String currentG = currentAcceleration/SensorManager.STANDARD_GRAVITY + "Gs"; accelerationTextView.setText(currentG); accelerationTextView.invalidate(); String maxG = maxAcceleration/SensorManager.STANDARD_GRAVITY + "Gs"; maxAccelerationTextView.setText(maxG); maxAccelerationTextView.invalidate(); } }); } } 

線程知識同樣不足的同學,我們一起把線程再學一遍吧,以後會持續更新相關的學習心得與大家分享。

差點忘了,還有 main.xml 檔案:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/acceleration" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text android:textSize="32sp" android:text="CENTER" android:editable="false" android:singleLine="true" android:layout_margin="10px"/> <TextView android:id="@+id/maxAcceleration" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text android:textSize="40sp" android:text="CENTER" android:editable="false" android:singleLine="true" android:layout_margin="10px"/> </LinearLayout> 

希望本文對大家的 Android 開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved