gpt4 book ai didi

java - 我怎样才能在我的 mainActivity 中实现这个类

转载 作者:行者123 更新时间:2023-12-01 13:36:21 25 4
gpt4 key购买 nike

我发现这个类使用指南针,但在我发现它的网站中,他们没有解释如何实现它,我的意思是,当我在主 Activity 中调用该类并将结果仅放入结果一次,并且不要用值不断刷新屏幕...这是我找到的类:

public class Compass implements SensorEventListener{

private SensorManager sensorManager;
private Sensor gsensor;
private Sensor msensor;
private float[] mGravity = new float[3];
private float[] mGeomagnetic = new float[3];
private float azimuth = 0f;
private float correctAzimuth = 0;

public Compass(Context context){
sensorManager = (SensorManager) context
.getSystemService(Context.SENSOR_SERVICE);
gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
msensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}

@Override
public void onSensorChanged(SensorEvent event) {
final float alpha = 0.97f;

synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

mGravity[0] = alpha * mGravity[0] + (1 - alpha)
* event.values[0];
mGravity[1] = alpha * mGravity[1] + (1 - alpha)
* event.values[1];
mGravity[2] = alpha * mGravity[2] + (1 - alpha)
* event.values[2];

// mGravity = event.values;
// Log.e(TAG, Float.toString(mGravity[0]));
}

if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
// mGeomagnetic = event.values;

mGeomagnetic[0] = alpha * mGeomagnetic[0] + (1 - alpha)
* event.values[0];
mGeomagnetic[1] = alpha * mGeomagnetic[1] + (1 - alpha)
* event.values[1];
mGeomagnetic[2] = alpha * mGeomagnetic[2] + (1 - alpha)
* event.values[2];
// Log.e(TAG, Float.toString(event.values[0]));

}

float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity,
mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
// Log.d(TAG, "azimuth (rad): " + azimuth);
azimuth = (float) Math.toDegrees(orientation[0]); // orientation
azimuth = (azimuth + 360) % 360;
// Log.d(TAG, "azimuth (deg): " + azimuth);
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

}

提前致谢!! :)

最佳答案

我尝试了代码,它对我有用 after我添加了

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

在 list 中。并添加了

    sensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL);

到 Compass 构造函数。

要访问罗盘值,您需要 Compass 类中方位角变量的 getter 方法。

Compass.onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Compass compass = new Compass(this);
}

主要 Activity :

package com.example.myfirstapp;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

public class Compass implements SensorEventListener{

private SensorManager sensorManager;
private Sensor gsensor;
private Sensor msensor;
private float[] mGravity = new float[3];
private float[] mGeomagnetic = new float[3];
private float azimuth = 0f;
private float correctAzimuth = 0;
private String TAG = "Compass";

public Compass(Context context)
{
sensorManager = (SensorManager) context
.getSystemService(Context.SENSOR_SERVICE);
gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
msensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

sensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent event)
{
final float alpha = 0.97f;

synchronized (this)
{
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{

mGravity[0] = alpha * mGravity[0] + (1 - alpha)
* event.values[0];
mGravity[1] = alpha * mGravity[1] + (1 - alpha)
* event.values[1];
mGravity[2] = alpha * mGravity[2] + (1 - alpha)
* event.values[2];

// mGravity = event.values;
// Log.e(TAG, Float.toString(mGravity[0]));
}

if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
{
// mGeomagnetic = event.values;

mGeomagnetic[0] = alpha * mGeomagnetic[0] + (1 - alpha)
* event.values[0];
mGeomagnetic[1] = alpha * mGeomagnetic[1] + (1 - alpha)
* event.values[1];
mGeomagnetic[2] = alpha * mGeomagnetic[2] + (1 - alpha)
* event.values[2];
// Log.e(TAG, Float.toString(event.values[0]));

}

float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.
getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success)
{
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
// Log.d(TAG, "azimuth (rad): " + azimuth);
azimuth = (float) Math.toDegrees(orientation[0]); // orientation
azimuth = (azimuth + 360) % 360;

Log.d(TAG, "azimuth (deg): " + azimuth);
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

}

关于java - 我怎样才能在我的 mainActivity 中实现这个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21240135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com