Android:在没有内存分配的情况下读取加速度计? [英] Android: reading accelerometer without memory allocation?

查看:20
本文介绍了Android:在没有内存分配的情况下读取加速度计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android (2.1+) 开发一款游戏,使用加速度计作为用户输入.我使用在活动开始时向传感器管理器注册的传感器侦听器,如下所示:

I am developing a game for Android (2.1+), using the accelerometer as user input. I use a sensor listener that I register at the beginning of the activity with the sensor manager, as follows:

mSensorManager.registerListener(SystemRegistry.inputSystem.mSensorListener,
                                accSensor, SensorManager.SENSOR_DELAY_UI);  

效果很好,我只是读取了 onSensorChanged(SensorEvent event) 中的加速度计值,以便在我的游戏循环中使用它:

That works well and I just read the accelerometer values in onSensorChanged(SensorEvent event) in order to use it in my game loop:

public void onSensorChanged(SensorEvent event){
     accX = event.values[0];
     accY = event.values[1];
     accY = event.values[2];
 }

我正在编写一个实时游戏,所以我试图避免在我的代码中分配任何内存,因为我想最小化垃圾收集.问题在于,每次出现 SensorEvent 时,传感器管理器实际上都会分配内存.例如,这里是 ddms 分配跟踪器的输出:

I am writing a real-time game so I am trying to avoid any memory allocation in my code, since I want to minimize the garbage collection. The problem is that every time there is a SensorEvent, the sensor manager actually allocates memory. Here is for example the output of the ddms allocation tracker:

51  28  android.hardware.SensorEvent    9   android.hardware.SensorManager$ListenerDelegate createSensorEvent   
50  28  float[] 9   android.hardware.SensorEvent    <init>  

显示每个事件分配的 28*2=56 字节.这似乎具有经常触发垃圾收集器的效果,这会导致一些滞后......所以这是我的问题:有没有办法在不分配任何内存的情况下实现同样的事情(获取当前加速度向量)?有没有办法在不等待事件的情况下实际按需读取此向量的值?

which shows 28*2=56 bytes allocated at every event. This seems to have the effect of triggering the garbage collector quite often, which induces some lags... So here is my question: is there a way to achieve the same thing (getting the current acceleration vector), without allocating any memory? Is there a way to actually read on demand the values of this vector without waiting for an event?

推荐答案

听起来像是我们需要解决的问题,我会在内部提交错误.

Sounds like something we need to fix on our side, I'll file a bug internally.

这篇关于Android:在没有内存分配的情况下读取加速度计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆