作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
您好,我想使用类型 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 ,但我听不懂(我是法语,我的英语不是很完美...)非常感谢您的帮助!!!
最佳答案
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();
}
}
关于java - 如何替换 android 4.0.3 的 type_orientation(已弃用)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15003787/
我是一名优秀的程序员,十分优秀!