嗨,在我的应用程序中,我需要创建一个陀螺仪,为此,我正在使用以下代码.
public class Accessgyroscope extends Activity implements SensorEventListener{ private TextVIEw tv; private SensorManager sManager; /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); tv = (TextVIEw) findVIEwByID(R.ID.tv); sManager = (SensorManager) getSystemService(SENSOR_SERVICE); } @OverrIDe protected voID onResume() { super.onResume(); sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST); } @OverrIDe protected voID onStop() { sManager.unregisterListener(this); super.onStop(); } @OverrIDe public voID onAccuracyChanged(Sensor arg0, int arg1) { } @OverrIDe public voID onSensorChanged(SensorEvent event) { if (event.accuracy == SensorManager.SENSOR_STATUS_UNREliABLE) { return; } tv.setText("OrIEntation X (Roll) :"+ float.toString(event.values[2]) +"\n"+ "OrIEntation Y (Pitch) :"+ float.toString(event.values[1]) +"\n"+ "OrIEntation Z (Yaw) :"+ float.toString(event.values[0])); }}
我可以拿到Yaw,Roll&螺距值,但我想在2d平面中显示.
基本上我想要这样的视图.
红点应根据设备的方向移动.我可以在画布上工作以准备该视图.但是我真正想要的是与Yaw,Roll& amp;相关的坐标.节距值.
请帮忙
解决方法:
终于我找到了答案.
首先,我使用TYPE_GraviTY而不是TYPE_ORIENTATION.
这是我的完整代码.
我的自定义视图的代码是..
public class gyroscopeVIEw extends VIEw{ private final static String TAG = "gyroscopeVIEw"; private float bearing; float pitch = 0; float roll = 0; private Paint paintOuter; private Paint paintInner; private Paint paintDot; float pointX, pointY; float dotX, dotY; int radius; public gyroscopeVIEw(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // Todo auto-generated constructor stub initCompassVIEw(); } public gyroscopeVIEw(Context context, AttributeSet attrs) { super(context, attrs); // Todo auto-generated constructor stub initCompassVIEw(); } public gyroscopeVIEw(Context context) { super(context); // Todo auto-generated constructor stub initCompassVIEw(); } protected voID initCompassVIEw() { setFocusable(true); Resources r = this.getResources(); paintOuter = new Paint(Paint.ANTI_AliAS_FLAG); paintOuter.setcolor(color.WHITE); paintOuter.setstrokeWIDth(1); paintOuter.setStyle(Paint.Style.FILL_AND_stroke); paintInner = new Paint(Paint.ANTI_AliAS_FLAG); paintInner.setcolor(color.BLUE); paintInner.setstrokeWIDth(1); paintInner.setStyle(Paint.Style.stroke); paintDot = new Paint(Paint.ANTI_AliAS_FLAG); paintDot.setcolor(color.RED); paintDot.setstrokeWIDth(1); paintDot.setStyle(Paint.Style.FILL_AND_stroke); } @OverrIDe protected voID onDraw(Canvas canvas) { // Todo auto-generated method stub super.onDraw(canvas); int px = getMeasureDWIDth() / 2; int py = getMeasuredHeight() / 2; radius = Math.min(px, py); pointX = px; pointY = py; canvas.drawCircle(pointX, pointY, radius, paintOuter); canvas.drawCircle(pointX, pointY, 40, paintInner); canvas.drawCircle(dotX, dotY, 5, paintDot); } voID update(float z, float yy, float xx) { if (yy > 0) { dotY = pointY - ((1 - yy) * z); } else { dotY = pointY + ((1 - yy) * z); } if (xx > 0) { dotX = pointX - ((1 - xx) * z); } else { dotX = pointX + ((1 - xx) * z); } invalIDate(); }
这是我的活动代码.
public class gyroscopeActivity extends Activity{ gyroscopeVIEw gyroscopeVIEw; SensorManager sensorManager; float[] gValues = new float[3]; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.gyroscope); gyroscopeVIEw = (gyroscopeVIEw) findVIEwByID(R.ID.gyroscope_vIEw); initSensor(); } voID initSensor() { sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); } @OverrIDe protected voID onResume() { super.onResume(); Sensor sensorgyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GraviTY); sensorManager.registerListener(sensorGyroListener, sensorgyroscope, SensorManager.SENSOR_DELAY_UI); } @OverrIDe protected voID onStop() { sensorManager.unregisterListener(sensorGyroListener); super.onStop(); } private final SensorEventListener sensorGyroListener = new SensorEventListener() { public voID onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_GraviTY) gValues = event.values; gyroscopeVIEw.update(gValues[0], gValues[1], gValues[2]); } public voID onAccuracyChanged(Sensor sensor, int accuracy) { } };
希望这会帮助某人.
总结以上是内存溢出为你收集整理的如何将偏航角,滚动和俯仰值转换为(x,y)坐标?全部内容,希望文章能够帮你解决如何将偏航角,滚动和俯仰值转换为(x,y)坐标?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)