安卓:读加速度计无内存分配? [英] Android: reading accelerometer without memory allocation?

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

问题描述

我正在开发一个游戏为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事件)的加速计值为了使用它在我的游戏循环:

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];
 }

我写一个实时游戏,所以我试图避免在我的code任何内存分配,因为我希望尽量减少垃圾收集。问题是,每次有一个 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.

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

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