得到倾斜角度的Andr​​oid [英] get tilt angle Android

查看:225
本文介绍了得到倾斜角度的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的传感器的倾斜角,横滚角从我的Andr​​oid手机,但没有成功,到目前为止,

当我点击我的按钮,应该给我3个角度,我得到成果:0.0 -0.0 -0.0

 包com.example;

进口android.app.Activity;
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;
进口android.os.Bundle;
进口android.widget.TextView;
进口android.widget.Button;
进口android.view.View;


公共类MyActivity扩展活动
{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        最终浮动[] mValues​​Magnet =新的浮动[3];
        最终浮动[] mValues​​Accel =新的浮动[3];
        最终浮动[] mValues​​Orientation =新的浮动[3];
        最终浮动[] mRotationMatrix =新的浮动[9];

        最终按钮btn_valider =(按钮)findViewById(R.id.btn1);
        最后的TextView TXT1 =(TextView中)findViewById(R.id.textView1);
        最后SensorEventListener mEventListener =新SensorEventListener(){
            公共无效onAccuracyChanged(传感器传感器,诠释精度){
            }

            公共无效onSensorChanged(SensorEvent事件){
                //处理为我们记录的事件
                开关(event.sensor.getType()){
                    案例Sensor.TYPE_ACCELEROMETER:
                        System.arraycopy(event.values​​,0,mValues​​Accel,0,3);
                        打破;

                    案例Sensor.TYPE_MAGNETIC_FIELD:
                        System.arraycopy(event.values​​,0,mValues​​Magnet,0,3);
                        打破;
                }
            };
        };
        btn_valider.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图查看)
            {
                SensorManager.getRotationMatrix(mRotationMatrix,空,mValues​​Accel,mValues​​Magnet);
                SensorManager.getOrientation(mRotationMatrix,mValues​​Orientation);
                最后CharSequence的测试;
                测试=结果:+ mValues​​Orientation [0] ++ mValues​​Orientation [1] ++ mValues​​Orientation [2];
                txt1.setText(试验);
            }
        });

    }
}
 

解决方案

您几乎没有用C迈克$ C $,但使数据被检索的事件侦听器的任何传感器,事件侦听器和传感器想必须与类的SensorManager使用注册的:

 的SensorManager _sensorManager;

_sensorManager.registerListener(mEventListener,_sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
                SensorManager.SENSOR_DELAY_NORMAL);
 

下面是你的code调整表明,我认为这是你希望看到什么样的数据。

 进口android.app.Activity;
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;
进口android.os.Bundle;
进口android.widget.TextView;
进口android.widget.Button;
进口android.view.View;


公共类StackOverflowTestingGroundActivity扩展活动
{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        的SensorManager的SensorManager =(的SensorManager)this.getSystemService(SENSOR_SERVICE);

        最终浮动[] mValues​​Magnet =新的浮动[3];
        最终浮动[] mValues​​Accel =新的浮动[3];
        最终浮动[] mValues​​Orientation =新的浮动[3];
        最终浮动[] mRotationMatrix =新的浮动[9];

        最终按钮btn_valider =(按钮)findViewById(R.id.btn1);
        最后的TextView TXT1 =(TextView中)findViewById(R.id.textView1);
        最后SensorEventListener mEventListener =新SensorEventListener(){
            公共无效onAccuracyChanged(传感器传感器,诠释精度){
            }

            公共无效onSensorChanged(SensorEvent事件){
                //处理为我们记录的事件
                开关(event.sensor.getType()){
                    案例Sensor.TYPE_ACCELEROMETER:
                        System.arraycopy(event.values​​,0,mValues​​Accel,0,3);
                        打破;

                    案例Sensor.TYPE_MAGNETIC_FIELD:
                        System.arraycopy(event.values​​,0,mValues​​Magnet,0,3);
                        打破;
                }
            };
        };

        //您已设置的情况下lisetner了,现在只需要与注册该
        //连同传感器通缉传感器管理器。
        setListners(的SensorManager,mEventListener);

        btn_valider.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图查看)
            {
                SensorManager.getRotationMatrix(mRotationMatrix,空,mValues​​Accel,mValues​​Magnet);
                SensorManager.getOrientation(mRotationMatrix,mValues​​Orientation);
                最后CharSequence的测试;
                测试=结果:+ mValues​​Orientation [0] ++ mValues​​Orientation [1] ++ mValues​​Orientation [2];
                txt1.setText(试验);
            }
        });

    }

    //注册事件侦听器和传感器类型。
    公共无效setListners(的SensorManager的SensorManager,SensorEventListener mEventListener)
    {
        sensorManager.registerListener(mEventListener,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
                SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(mEventListener,sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
                SensorManager.SENSOR_DELAY_NORMAL);
    }
}
 

I'm trying to get the tilt angle, roll angle from my sensors from my android phone but with no success so far,

when I click on my button which should give me the 3 angles, I get "results: 0.0 -0.0 -0.0"

package com.example;

import android.app.Activity;
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 android.widget.Button;
import android.view.View;


public class MyActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final float[] mValuesMagnet      = new float[3];
        final float[] mValuesAccel       = new float[3];
        final float[] mValuesOrientation = new float[3];
        final float[] mRotationMatrix    = new float[9];

        final Button btn_valider = (Button) findViewById(R.id.btn1);
        final TextView txt1 = (TextView) findViewById(R.id.textView1);
        final SensorEventListener mEventListener = new SensorEventListener() {
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }

            public void onSensorChanged(SensorEvent event) {
                // Handle the events for which we registered
                switch (event.sensor.getType()) {
                    case Sensor.TYPE_ACCELEROMETER:
                        System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
                        break;

                    case Sensor.TYPE_MAGNETIC_FIELD:
                        System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
                        break;
                }
            };
        };
        btn_valider.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
                SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
                final CharSequence test;
                test = "results: " + mValuesOrientation[0] +" "+mValuesOrientation[1]+ " "+ mValuesOrientation[2];
                txt1.setText(test);
            }
        });

    }
}

解决方案

Your almost there with the code Mike, but to enable the data to be retrieved by the event listener for any of the sensors, the event listener and sensor wanted must be registered with the SensorManager class using:

SensorManager _sensorManager;

_sensorManager.registerListener(mEventListener, _sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
                SensorManager.SENSOR_DELAY_NORMAL);

Below is your code adapted to show the data that I believe is what you want to see.

import android.app.Activity;
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 android.widget.Button;
import android.view.View;


public class StackOverflowTestingGroundActivity extends Activity
{   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

        SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);        

        final float[] mValuesMagnet      = new float[3];
        final float[] mValuesAccel       = new float[3];
        final float[] mValuesOrientation = new float[3];
        final float[] mRotationMatrix    = new float[9];

        final Button btn_valider = (Button) findViewById(R.id.btn1);
        final TextView txt1 = (TextView) findViewById(R.id.textView1);
        final SensorEventListener mEventListener = new SensorEventListener() {
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }

            public void onSensorChanged(SensorEvent event) {
                // Handle the events for which we registered
                switch (event.sensor.getType()) {
                    case Sensor.TYPE_ACCELEROMETER:
                        System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
                        break;

                    case Sensor.TYPE_MAGNETIC_FIELD:
                        System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
                        break;
                }
            };
        };

        // You have set the event lisetner up, now just need to register this with the
        // sensor manager along with the sensor wanted.
        setListners(sensorManager, mEventListener);

        btn_valider.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
                SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
                final CharSequence test;
                test = "results: " + mValuesOrientation[0] +" "+mValuesOrientation[1]+ " "+ mValuesOrientation[2];
                txt1.setText(test);
            }
        });

    }

    // Register the event listener and sensor type.
    public void setListners(SensorManager sensorManager, SensorEventListener mEventListener)
    {
        sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
                SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
                SensorManager.SENSOR_DELAY_NORMAL);
    }
}

这篇关于得到倾斜角度的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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