0

我想检测我的智能手机用户是左转还是右转或转身。

这里举个例子来解释“右转”:

用户面临一个十字路口。现在我想检测我的用户是走右路还是走左路还是直走。

因此我用它来获得方位角:

    @Override
public void onSensorChanged(SensorEvent event) {

    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        for (int i = 0; i < 3; i++) {
            valuesAccelerometer[i] = event.values[i];
        }
        break;
    case Sensor.TYPE_MAGNETIC_FIELD:
        for (int i = 0; i < 3; i++) {
            valuesMagneticField[i] = event.values[i];
        }
        break;
    }
    boolean success = SensorManager.getRotationMatrix(matrixR, matrixI,
            valuesAccelerometer, valuesMagneticField);

    if (success) {
        SensorManager.getOrientation(matrixR, matrixValues);
        Math.round(Math.toDegrees(Math.acos(matrixValues[0])));
        azimuth = (float) Math.round(Math.toDegrees(matrixValues[0]));
        if (azimuth < 0.0f) {
            azimuth += 360.0f;
        }
   }

这工作正常。但现在我不知道如何设置我的旋转的起点和终点......

4

1 回答 1

2

使用这个程序。每当您向左或向右和向上和向下转动时,您都会收到像箭头图像一样的水平和垂直通知..您将获得该坐标。

 package com.authorwjf;

 import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.content.Context;
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.TextView;

 @SuppressLint("NewApi")
 public class Main extends Activity implements SensorEventListener {

private float mLastX, mLastY, mLastZ;
private boolean mInitialized;
private SensorManager mSensorManager;
 private Sensor mAccelerometer;
 private final float NOISE = (float) 2.0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mInitialized = false;
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this,
  mAccelerometer ,  SensorManager.SENSOR_DELAY_NORMAL);
 }

protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, 
 mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
 }

 protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
 }

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // can be safely ignored for this demo
}

@Override
public void onSensorChanged(SensorEvent event) {
    TextView tvX= (TextView)findViewById(R.id.x_axis);
    TextView tvY= (TextView)findViewById(R.id.y_axis);
    TextView tvZ= (TextView)findViewById(R.id.z_axis);
    ImageView iv = (ImageView)findViewById(R.id.image);
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    if (!mInitialized) {
        mLastX = x;
        mLastY = y;
        mLastZ = z;
        tvX.setText("0.0");
        tvY.setText("0.0");
        tvZ.setText("0.0");
        mInitialized = true;
    } else {
        float deltaX = Math.abs(mLastX - x);
        float deltaY = Math.abs(mLastY - y);
        float deltaZ = Math.abs(mLastZ - z);
        if (deltaX < NOISE) deltaX = (float)0.0;
        if (deltaY < NOISE) deltaY = (float)0.0;
        if (deltaZ < NOISE) deltaZ = (float)0.0;
        mLastX = x;
        mLastY = y;
        mLastZ = z;
        tvX.setText(Float.toString(deltaX));
        tvY.setText(Float.toString(deltaY));
        tvZ.setText(Float.toString(deltaZ));
        iv.setVisibility(View.VISIBLE);
        if (deltaX > deltaY) {
            iv.setImageResource(R.drawable.horizontal);
        } else if (deltaY > deltaX) {
            iv.setImageResource(R.drawable.vertical);
        } else {
            iv.setVisibility(View.INVISIBLE);
        }
    }
}
 }

主要的.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView  
    android:paddingTop="20dip"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textSize="16sp" 
    android:textStyle="bold"
    android:gravity="center"
    android:text="Shaker Demo"/>
<TableLayout
    android:paddingTop="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*">
    <TableRow>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textSize="14sp"  
            android:text="X-Axis"
            android:gravity="center"/>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:textSize="14sp" 
            android:text="Y-Axis"
            android:gravity="center"/>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textSize="14sp"  
            android:text="Z-Axis"
            android:gravity="center"/>
    </TableRow>
    <TableRow>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/x_axis"
            android:gravity="center"/>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/y_axis"
            android:gravity="center"/>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/z_axis"
            android:gravity="center"/>
    </TableRow>
</TableLayout>
<ImageView
    android:paddingTop="10dip"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:id="@+id/image"
    android:layout_gravity="center"
    android:visibility="invisible"/>
</LinearLayout>
于 2013-07-16T15:31:48.237 回答