简体   繁体   English

LibGDX光传感器数据

[英]LibGDX light sensor data

I am using LibGDX to make a desktop/android application. 我正在使用LibGDX制作桌面/ Android应用程序。 In part of my application I am checking the light sensor if it is android. 在我的应用程序的一部分中,我正在检查光传感器是否为android。 This works fine for android except when I implement it into my LibGDX project I am confused as to how I can pass this data across. 这对于android来说很好用,除非当我将其实现到我的LibGDX项目中时,我对如何传递数据感到困惑。

SensorManager sm;
Sensor s;
float lux = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
s = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
initialize(new MyLibGDXMain(), cfg);
}

@Override
public void onSensorChanged(SensorEvent event) {
lux = event.values[0];
}

I can retrieve my light sensor data here but once I initialize MyLibGDXMain I lose this data. 我可以在这里检索我的光传感器数据,但是一旦初始化MyLibGDXMain,我就会丢失这些数据。 If I add the SensorEventListener to that class I lose compatibility with the desktop version. 如果将SensorEventListener添加到该类,则将失去与桌面版本的兼容性。 So I have tried to create another constructor for the MyLibGDXMain to pass the lux value over but this only breaks whatever the LibGDX application is doing. 因此,我尝试为MyLibGDXMain创建另一个构造函数以传递lux值,但这只会破坏LibGDX应用程序正在执行的操作。

This question is not how to retrieve sensor data on android but rather How would I go about getting this sensor data into my LibGDX main without breaking compatibility with the desktop project? 这个问题不是如何在android上检索传感器数据,而是如何在不破坏与桌面项目的兼容性的情况下将该传感器数据放入我的LibGDX主文件中?

You will need to create an interface between your application and the platform-specific code... 您将需要在应用程序和特定于平台的代码之间创建接口。

https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code

One way would be to create an Interface LightSensorInterface in your main project, containing a method float getCurrentLux(); 一种方法是在主项目中创建一个接口LightSensorInterface ,其中包含一个方法float getCurrentLux(); LightSensorInterface

public interface LightSensorInterface {
    public float getCurrentLux();
}

You then would have to create a class that implements that interface in your android project and one in your desktop project as well. 然后,您将不得不创建一个类,该类在您的android项目中实现该接口,并且在您的桌面项目中也实现该接口。

public class AndroidLightSensor implements LightSensorInterface, SensorEventListener {
    private float currentLux = 0;

    @Override
    public float getCurrentLux() {
        return currentLux;
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        this.currentLux = event.values[0];
    }
}

Since you don't have a light sensor on your desktop application, the implemented method there could just return some fixed value. 由于您的桌面应用程序上没有光传感器,因此此处实现的方法可能只返回一些固定值。

And the implemented getCurrentLux() in the android project needs to return the lux value that you got the last time onSensorChanged() was called... android项目中实现的getCurrentLux()需要返回您上次调用onSensorChanged()时获得的勒克斯值。

The constructor of your MyLibGDXMain() will need a reference to a LightSensorInterface and when initializing your app, you would pass the platform-specific class: MyLibGDXMain()的构造函数将需要对LightSensorInterface的引用,并且在初始化应用程序时,您将传递平台特定的类:

onCreate() {
     ....
    AndroidLightSensor sensor = new AndroidLightSensor(...);
    initialize(new MyLibGDXMain(sensor);
}

If you want to always have the current value, you need to fetch the current value within your render loop... hope you get what I mean... 如果您希望始终拥有当前值,则需要在渲染循环中获取当前值...希望您能理解我的意思...

Hope it helps.. ::) 希望能帮助到你.. ::)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM