如何在Java中创建一个Android类,以返回设备角度。

huangapple go评论139阅读模式
英文:

How to make an Android class in java that returns device angle

问题

我正在尝试制作一个使用设备角度的应用程序。我正在努力找出如何创建一个带有函数的类,我可以在另一个文件中调用该函数,以返回设备的Y轴角度。我以前从未使用过传感器,并且在尝试设置它们时遇到了问题。我将从中调用该函数的文件正在前台运行。是否有人知道如何做到这一点并且可以分享一些代码?谢谢!

英文:

I am trying to make an app that uses the devices angle. I am trying to figure out how to make a class with a function that I can call in another file that will return the devices Y angle. I have never used sensor before and I have had trouble trying to set them up. The file I will be calling the function from is running in the foreground. Does anyone know how to do this and can share some code? Thank You!

答案1

得分: 0

这是您提供的代码的翻译:

你可以尝试查看Android Google CodeLabs。此外,我在互联网上找到了一个解决方案,稍微进行了修改,但我不确定显示的数据是否始终正确。此示例应用程序始终跟踪设备的旋转,因此,如果您想要一个返回旋转角度的函数,您需要稍微重构代码。

MainActivity.java:

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private TextView textViewToDisplayRotation;
    private float[] acc = new float[3];
    private float[] mags = new float[3];
    private float[] values = new float[3];
    SensorManager sManager;
    private SensorEventListener mySensorEventListener = new SensorEventListener() {
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        public void onSensorChanged(SensorEvent event) {
            switch (event.sensor.getType()) {
                case Sensor.TYPE_MAGNETIC_FIELD:
                    mags = event.values.clone();
                    break;
                case Sensor.TYPE_ACCELEROMETER:
                    acc = event.values.clone();
                    break;
            }

            if (mags != null && acc != null) {
                float[] gravity = new float[9];
                float[] magnetic = new float[9];
                SensorManager.getRotationMatrix(gravity, magnetic, acc, mags);
                float[] outGravity = new float[9];
                SensorManager.remapCoordinateSystem(gravity,
                        SensorManager.AXIS_X,
                        SensorManager.AXIS_Z,
                        outGravity);
                SensorManager.getOrientation(outGravity, values);

                float azimuth = values[0] * 57.2957795f;
                float pitch = values[1] * 57.2957795f;
                float roll = values[2] * 57.2957795f;
                textViewToDisplayRotation.setText("X = " + azimuth + "\nY = " + pitch + "\nZ = " + roll);
                mags = null;
                acc = null;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        textViewToDisplayRotation = findViewById(R.id.textViewToDisplayRotation);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sManager.registerListener(mySensorEventListener,
                sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        sManager.registerListener(mySensorEventListener,
                sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_NORMAL);
    }
}

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textViewToDisplayRotation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textSize="40sp" />
英文:

You can try to check Android google CodeLabs. Also I found on the internet one solution, I changed it a little bit but I am not sure if the displayed data is always correct. This sample app tracks device rotation all the time so If You want to have a function that returns rotation You have to refactor code a little.

MainActivity.java:

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
private TextView textViewToDisplayRotation;
private float[] acc = new float[3];
private float[] mags = new float[3];
private float[] values = new float[3];
SensorManager sManager;
private SensorEventListener mySensorEventListener = new SensorEventListener()
{
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
public void onSensorChanged(SensorEvent event)
{
switch (event.sensor.getType())
{
case Sensor.TYPE_MAGNETIC_FIELD:
mags = event.values.clone();
break;
case Sensor.TYPE_ACCELEROMETER:
acc = event.values.clone();
break;
}
if (mags != null &amp;&amp; acc != null)
{
float[] gravity = new float[9];
float[] magnetic = new float[9];
SensorManager.getRotationMatrix(gravity, magnetic, acc, mags);
float[] outGravity = new float[9];
SensorManager.remapCoordinateSystem(gravity,
SensorManager.AXIS_X,
SensorManager.AXIS_Z,
outGravity);
SensorManager.getOrientation(outGravity, values);
float azimuth = values[0] * 57.2957795f;
float pitch = values[1] * 57.2957795f;
float roll = values[2] * 57.2957795f;
textViewToDisplayRotation.setText(&quot;X = &quot; + azimuth + &quot;\nY = &quot; + pitch + &quot;\nZ = &quot; + roll);
mags = null;
acc = null;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
textViewToDisplayRotation = findViewById(R.id.textViewToDisplayRotation);
}
@Override
protected void onResume()
{
super.onResume();
sManager.registerListener(mySensorEventListener,
sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sManager.registerListener(mySensorEventListener,
sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
}
}

main_activity.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;TextView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:id=&quot;@+id/textViewToDisplayRotation&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:gravity=&quot;center&quot;
android:textSize=&quot;40sp&quot; /&gt;

huangapple
  • 本文由 发表于 2020年8月17日 07:20:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63442812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定