如何为 android 4.0.3 替换 type_orientation(已弃用)? [英] How can i replace type_orientation (is deprecated) for android 4.0.3?

查看:33
本文介绍了如何为 android 4.0.3 替换 type_orientation(已弃用)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I want to use the type type_orientation but it is deprecated for the android 4.0.3 . I use it for the augmented reality. I tried to find on the net but without success. My application works but my text inside stay in the same place! Here my code:

 @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/参考/android/hardware/Sensor.html,但我看不懂(我是法语,我的英语不是很完美...)非常感谢您的帮助!!!

Everything it s good just this, i looked already on http://developer.android.com/reference/android/hardware/Sensor.html, but i can t understand (I m french and my english is not very perfect...) Thank you very much for your help!!!

推荐答案

这里有一个链接 指向带有解决方案的博客.本质上,您停止使用方向传感器并同时使用磁场传感器和加速度计传感器以获得等效功能.这是更多的工作,但它确实允许您继续使用回调来处理方向更改.

Here's a link to a blog with a solution. Essentially, you stop using the Orientation Sensor and use the Magnetic Field Sensor and Accelerometer Sensor(s) in tandem to get equivalent functionality. It's more work but it does allow you to continue to use a callback to handle orientation changes.

由于原始站点不再可用,这里是以前托管在那里的代码示例:

Since the original site is no longer available, here's the code example that was previously hosted there:

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();
  }
}

这篇关于如何为 android 4.0.3 替换 type_orientation(已弃用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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