【Android筆記 六】Android Sensor感應器介紹(二)線程中重新整理UI 一個建立android測力計的例子

來源:互聯網
上載者:User

  上一篇文章http://blog.csdn.net/octobershiner/article/details/6639040  介紹了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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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_GRAVITYdouble 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="http://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:textStyle="bold"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:textStyle="bold"android:textSize="40sp"android:text="CENTER"android:editable="false"android:singleLine="true"android:layout_margin="10px"/></LinearLayout>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.