当前位置: 移动技术网 > 移动技术>移动开发>Android > Android使用Sensor感应器实现线程中刷新UI创建android测力计的功能

Android使用Sensor感应器实现线程中刷新UI创建android测力计的功能

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android使用sensor感应器实现线程中刷新ui创建android测力计的功能。分享给大家供大家参考,具体如下:

前面一篇《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="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> 

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网