Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測(cè)力計(jì)的功能
本文實(shí)例講述了Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測(cè)力計(jì)的功能。分享給大家供大家參考,具體如下:
前面一篇《Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法》我們介紹了sensor的基本知識(shí)以及一個(gè)使用其中加速度感應(yīng)器獲取數(shù)據(jù)的例子。
前面提到過(guò)一個(gè)問(wèn)題,就是說(shuō)感應(yīng)器刷新頻率太快,假如我們要做一個(gè)UI中,需要根據(jù)方向數(shù)據(jù)繪制一個(gè)一個(gè)移動(dòng)的箭頭,那么就要太過(guò)頻繁的刷新繪制界面,占用很多的資源,體驗(yàn)性也會(huì)很差,《android 2高級(jí)編程》中一個(gè)演示測(cè)力器的例子,卻無(wú)意中給我們提供了一種此情況下刷新UI的解決方案,這下我們就知道了如何防止感應(yīng)器在界面中過(guò)于頻繁的刷新。
下面是自己修改的代碼,供大家參考
/* * @author octobershiner * 2011 07 27 * SE.HIT * 這是《Android 2 高級(jí)編程》中的一個(gè)實(shí)例,關(guān)于感應(yīng)器的使用很普通,但是介紹了一種使用感應(yīng)器的應(yīng)用如何刷新UI的好辦法,值得學(xué)習(xí) * 我添加了一些注釋和onPause方法 * 一個(gè)演示感應(yīng)器在線程中刷新UI的例子 測(cè)力器的應(yīng)用 * */ 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); //獲取兩個(gè)文本顯示域 accelerationTextView = (TextView)findViewById(R.id.acceleration); maxAccelerationTextView = (TextView)findViewById(R.id.maxAcceleration); //獲取sensor服務(wù),選擇加速度感應(yīng)器 sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //注冊(cè)事件 sensorManager.registerListener(sensorEventListener, accelerometer, SensorManager.SENSOR_DELAY_FASTEST); Timer updateTimer = new Timer("gForceUpdate"); updateTimer.scheduleAtFixedRate(new TimerTask() { public void run() { updateGUI(); } }, 0, 100); } //添加的新方法,退出activity的時(shí)候,關(guān)閉監(jiān)聽(tīng)器 public void onPause(){ sensorManager.unregisterListener(sensorEventListener); super.onPause(); } private final SensorEventListener sensorEventListener = new SensorEventListener() { //系統(tǒng)設(shè)置的重力加速度標(biāo)準(zhǔn)值,設(shè)備在水平靜止的情況下就承受這個(gè)壓力,所以默認(rèn)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]; //計(jì)算三個(gè)方向的加速度 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() { /* * 推薦的一個(gè)刷新UI的方法 * Activity.runOnUiThread(Runnable) * 在新的線程中更新UI * Runnable是一個(gè)接口,需要你實(shí)現(xiàn)run方法,上面的TimerTask就是實(shí)現(xiàn)了這個(gè)接口同樣需要實(shí)現(xiàn)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(); } }); } }
線程知識(shí)和我一樣不足的同學(xué),我們一起再學(xué)習(xí)線程吧,以后會(huì)更新相關(guān)的學(xué)習(xí)體會(huì),與大家分享
忘了,還有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>
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android利用Sensor(傳感器)實(shí)現(xiàn)水平儀功能
- Android使用Sensor感應(yīng)器獲取用戶(hù)移動(dòng)方向(指南針原理)
- Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法
- android 傳感器(OnSensorChanged)使用介紹
- 解析如何在android中增加gsensor驅(qū)動(dòng)(MMA7660)
- Android利用方向傳感器獲得手機(jī)的相對(duì)角度實(shí)例說(shuō)明
- Android 利用方向傳感器實(shí)現(xiàn)指南針具體步驟
- Android重力傳感器實(shí)現(xiàn)滾動(dòng)的彈球
- Android 傳感器--光照傳感器詳解及使用
- Android利用Sensor(傳感器)實(shí)現(xiàn)指南針小功能
相關(guān)文章
Android實(shí)現(xiàn)微信自動(dòng)向附近的人打招呼(AccessibilityService)
這篇文章主要為大家詳細(xì)介紹了實(shí)現(xiàn)微信自動(dòng)向附近的人打招呼,實(shí)現(xiàn)收到指定賬戶(hù)推送文章時(shí)自動(dòng)進(jìn)入微信打開(kāi)鏈接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10Android中ArrayList和數(shù)組相互轉(zhuǎn)換
在我們?nèi)粘i_(kāi)發(fā)中難免會(huì)要將ArrayList和數(shù)組相互轉(zhuǎn)換,那么如何才能相互轉(zhuǎn)換呢?下面跟著小編一起通過(guò)這篇文章學(xué)習(xí)學(xué)習(xí)。2016-08-08Android開(kāi)發(fā)實(shí)現(xiàn)瀏覽器全屏顯示功能
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)瀏覽器全屏顯示功能,涉及Android布局修改及相關(guān)屬性動(dòng)態(tài)設(shè)置操作技巧,需要的朋友可以參考下2017-09-09Android6.0動(dòng)態(tài)申請(qǐng)權(quán)限所遇到的問(wèn)題小結(jié)
這篇文章給大家介紹了Android6.0動(dòng)態(tài)申請(qǐng)權(quán)限所遇到的問(wèn)題,在沒(méi)給大家介紹這下問(wèn)題之前,先給大家說(shuō)下基本定義和基本使用方式,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,對(duì)android 6.0 動(dòng)態(tài)權(quán)限遇到問(wèn)題感興趣的朋友一起看看吧2016-11-11Android 網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽(tīng)代碼實(shí)例(一)
本文給大家介紹Android 網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽(tīng)代碼實(shí)例(一),對(duì)android網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng)相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03Android RecyclerView網(wǎng)格布局示例解析
這篇文章主要介紹了Android RecyclerView網(wǎng)格布局示例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12