【问题标题】:How can i replace type_orientation (is deprecated) for android 4.0.3?如何替换 android 4.0.3 的 type_orientation(已弃用)?
【发布时间】:2013-02-21 13:43:02
【问题描述】:

您好,我想使用 type_orientation 类型,但它已被 android 4.0.3 弃用。我将它用于增强现实。我试图在网上找到但没有成功。我的应用程序有效,但我里面的文字留在同一个地方!这是我的代码:

 @Override
    protected void onStart() {      
        super.onStart();
        sensorMngr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorLstr = createListener();
        sensorMngr.registerListener(sensorLstr, sensorMngr.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onStop() {       
        super.onStop();
        sensorMngr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorMngr.unregisterListener(sensorLstr, sensorMngr.getDefaultSensor(Sensor.TYPE_ORIENTATION));        
    }

一切都很好,我已经看过http://developer.android.com/reference/android/hardware/Sensor.html,但我听不懂(我是法国人,我的英语不是很完美......)非常感谢你的帮助!!!

【问题讨论】:

    标签: java android eclipse android-sensors


    【解决方案1】:

    Here's a link 到一个有解决方案的博客。从本质上讲,您停止使用方向传感器并同时使用磁场传感器和加速度传感器来获得等效的功能。它的工作量更大,但它确实允许您继续使用回调来处理方向更改。

    编辑:由于原始站点不再可用,以下是之前托管的代码示例:

    package com.samplecompass;
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Paint.Style;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.view.View;
    
    public class CompassActivity extends Activity implements SensorEventListener {
    
      Float azimut;  // View to draw a compass
    
      public class CustomDrawableView extends View {
        Paint paint = new Paint();
        public CustomDrawableView(Context context) {
          super(context);
          paint.setColor(0xff00ff00);
          paint.setStyle(Style.STROKE);
          paint.setStrokeWidth(2);
          paint.setAntiAlias(true);
        };
    
        protected void onDraw(Canvas canvas) {
          int width = getWidth();
          int height = getHeight();
          int centerx = width/2;
          int centery = height/2;
          canvas.drawLine(centerx, 0, centerx, height, paint);
          canvas.drawLine(0, centery, width, centery, paint);
          // Rotate the canvas with the azimut      
          if (azimut != null)
            canvas.rotate(-azimut*360/(2*3.14159f), centerx, centery);
          paint.setColor(0xff0000ff);
          canvas.drawLine(centerx, -1000, centerx, +1000, paint);
          canvas.drawLine(-1000, centery, 1000, centery, paint);
          canvas.drawText("N", centerx+5, centery-10, paint);
          canvas.drawText("S", centerx-10, centery+15, paint);
          paint.setColor(0xff00ff00);
        }
      }
    
      CustomDrawableView mCustomDrawableView;
      private SensorManager mSensorManager;
      Sensor accelerometer;
      Sensor magnetometer;
    
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCustomDrawableView = new CustomDrawableView(this);
        setContentView(mCustomDrawableView);    // Register the sensor listeners
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
          accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
      }
    
      protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
        mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
      }
    
      protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
      }
    
      public void onAccuracyChanged(Sensor sensor, int accuracy) {  }
    
      float[] mGravity;
      float[] mGeomagnetic;
      public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
          mGravity = event.values;
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
          mGeomagnetic = event.values;
        if (mGravity != null && mGeomagnetic != null) {
          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);
            azimut = orientation[0]; // orientation contains: azimut, pitch and roll
          }
        }
        mCustomDrawableView.invalidate();
      }
    }
    

    【讨论】:

    • 现在可以了,我忘了把方位角转换成度数。谢谢!
    • 我猜ORIENTATION_SENSOR 在所有设备中都很常见!磁力计和加速度计也常见吗?
    • 不幸的是,磁力计不是通用传感器。例如,Moto G3(第 3 代)没有。
    • 网页不可用
    • 我找到了该网站的cached version,并将源示例从那里添加到这篇文章中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2020-02-27
    • 2018-07-05
    • 1970-01-01
    • 2014-01-10
    • 2019-12-05
    相关资源
    最近更新 更多