如何在android studio 上用加速度传感器来得到x y z轴上的坐标?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在android studio 上用加速度传感器来得到x y z轴上的坐标?相关的知识,希望对你有一定的参考价值。

最好有代码

参考技术A public class MainActivity extends Activity implements SensorEventListener
private static final String TAG = MainActivity.class.getSimpleName();
private SensorManager mSensorManager;
private Sensor mSensor;
private TextView textviewX;
private TextView textviewY;
private TextView textviewZ;
private TextView textviewF;
private int mX, mY, mZ;
private long lasttimestamp = 0;
Calendar mCalendar;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textviewX = (TextView) findViewById(R.id.textView1);
textviewY = (TextView) findViewById(R.id.textView3);
textviewZ = (TextView) findViewById(R.id.textView4);
textviewF = (TextView) findViewById(R.id.textView2);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);// TYPE_GRAVITY
if (null == mSensorManager)
Log.d(TAG, "deveice not support SensorManager");

// 参数三,检测的精准度
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);// SENSOR_DELAY_GAME

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

@Override
public void onSensorChanged(SensorEvent event)
if (event.sensor == null)
return;

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
int x = (int) event.values[0];
int y = (int) event.values[1];
int z = (int) event.values[2];
mCalendar = Calendar.getInstance();
long stamp = mCalendar.getTimeInMillis() / 1000l;// 1393844912
textviewX.setText(String.valueOf(x));
textviewY.setText(String.valueOf(y));
textviewZ.setText(String.valueOf(z));
int second = mCalendar.get(Calendar.SECOND);// 53
int px = Math.abs(mX - x);
int py = Math.abs(mY - y);
int pz = Math.abs(mZ - z);
Log.d(TAG, "pX:" + px + " pY:" + py + " pZ:" + pz + " stamp:"
+ stamp + " second:" + second);
int maxvalue = getMaxValue(px, py, pz);
if (maxvalue > 2 && (stamp - lasttimestamp) > 30)
lasttimestamp = stamp;
Log.d(TAG, " sensor isMoveorchanged....");
textviewF.setText("检测手机在移动..");

mX = x;
mY = y;
mZ = z;


/**
* 获取一个最大值
*
* @param px
* @param py
* @param pz
* @return
*/
public int getMaxValue(int px, int py, int pz)
int max = 0;
if (px > py && px > pz)
max = px;
else if (py > px && py > pz)
max = py;
else if (pz > px && pz > py)
max = pz;

return max;

在 android studio 应用程序中读取磁力计传感器

【中文标题】在 android studio 应用程序中读取磁力计传感器【英文标题】:Magnetometer sensors reading in android studio application 【发布时间】:2018-09-18 09:26:46 【问题描述】:

如何在 android 应用程序中获取x y z 坐标中的磁力计读数?

【问题讨论】:

【参考方案1】:

这是一个关于如何检索传感器值的示例:

public class MagnetometerActivity extends Activity implements SensorEventListener 

    // Sensors & SensorManager
    private Sensor magnetometer;
    private SensorManager mSensorManager;

    // Storage for Sensor readings
    private float[] mGeomagnetic = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get a reference to the SensorManager
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        // Get a reference to the magnetometer
        magnetometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        // Exit unless sensor are available
        if (null == magnetometer)
            finish();

    

    @Override
    protected void onResume() 
        super.onResume();

        // Register for sensor updates

        mSensorManager.registerListener(this, magnetometer,
                SensorManager.SENSOR_DELAY_NORMAL);
    

    @Override
    protected void onPause() 
        super.onPause();

        // Unregister all sensors
        mSensorManager.unregisterListener(this);

    

    @Override
    public void onSensorChanged(SensorEvent event) 

        // Acquire magnetometer event data

        else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) 

            mGeomagnetic = new float[3];
            System.arraycopy(event.values, 0, mGeomagnetic, 0, 3);

        

        // If we have readings from both sensors then
        // use the readings to compute the device's orientation
        // and then update the display.

        if (mGeomagnetic != null) 
            Log.d(TAG, "mx : "+mGeomagnetic[0]+" my : "+mGeomagnetic[1]+" mz : "+mGeomagnetic[2]);
        

    

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) 
        // N/A
    

如果您想要磁力计的未校准值,您可以使用TYPE_MAGNETIC_FIELD_UNCALIBRATED 传感器类型。请找到所有传感器的详细信息here。

希望对您有所帮助!

【讨论】:

以上是关于如何在android studio 上用加速度传感器来得到x y z轴上的坐标?的主要内容,如果未能解决你的问题,请参考以下文章

如何在android的驱动程序中对加速度传感器的数据进行方向和坐标的转

Android移动开发视频之传感器应用开发

在 Apple 芯片设备上用 Android Studio?别忘了使用 Apple 芯片预览版!

在 Apple 芯片设备上用 Android Studio?别忘了使用 Apple 芯片预览版!

在Mac上用bootcamp安装windows,使用Android studio启动模拟器时蓝屏问题的解决方法

Android中的“重力”和“加速度”传感器有啥区别?