Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java指南针给我疯狂的数据?

我有一个关于安卓中compass的项目

它只显示手机相对于Z轴的角度,如下所示(角度是根据跟随度计算的) enter image description here

这是我的密码

主要活动。爪哇

package com.example.sensormagnet;

import 安卓.hardware.Sensor;
import 安卓.hardware.SensorEvent;
import 安卓.hardware.SensorEventListener;
import 安卓.hardware.SensorManager;
import 安卓.os.Bundle;
import 安卓.app.Activity;
import 安卓.view.Menu;
import 安卓.webkit.WebView;
import 安卓.widget.EditText;
import 安卓.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {
float[] mGravity = new float[3];
float[] mGeomagnetic = new float[3];
Sensor accelerometer;
Sensor orientationSensor;
EditText et1;
TextView tw1;
SensorManager mSensorManager;
static float azimuth;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView)findViewById(R.id.web);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(true);
    graphic gra = new graphic(webView.getContext());
    webView.addView(gra);
    et1 = (EditText)findViewById(R.id.editText1);
    //phai khai bao 1 bien sensormanager
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    //khai bao loai sensor minh dung
     accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
     orientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mSensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        for(int i =0; i < 3; i++){
            mGravity[i] = event.values[i];
           }
        }


        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            for(int i =0; i < 3; i++){
                mGeomagnetic[i] = event.values[i];
               }
        }

        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);   
                azimuth = (float) Math.toDegrees(orientation[0]);
                et1.setText(String.valueOf(azimuth));
            }
        }
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    mSensorManager.unregisterListener(this,
            accelerometer);
    mSensorManager.unregisterListener(this,
            orientationSensor); }
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    onPause();
}


}

图形。爪哇

    package com.example.sensormagnet;

    import java.net.URL;

    import 安卓.content.Context;
    import 安卓.graphics.Canvas;
    import 安卓.graphics.Color;
    import 安卓.graphics.Paint;
    import 安卓.view.View;

     public class graphic extends View{
    Paint p = new Paint();


public graphic(Context context) {
    super(context);
    // TODO Auto-generated constructor stub

}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    p.setColor(Color.RED);
    canvas.drawCircle(200, 200, 5, p);
    p.setColor(Color.GREEN);
    p.setStyle(Paint.Style.STROKE);
    p.setStrokeWidth(5);
    canvas.drawLine(200, 200, (float) (30*Math.cos(Math.toRadians(MainActivity.azimuth))+200), (float) (30*Math.sin(Math.toRadians(MainActivity.azimuth))+200), p);
    invalidate();

}

}

布局

<WebView
    安卓:id="@+id/web"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:layout_alignParentRight="true"
    安卓:layout_below="@+id/editText1" />

<EditText
    安卓:id="@+id/editText1"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignBottom="@+id/textView1"
    安卓:layout_alignParentRight="true"
    安卓:ems="10" />

<TextView
    安卓:id="@+id/textView1"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignParentTop="true"
    安卓:layout_marginRight="23dp"
    安卓:layout_marginTop="20dp"
    安卓:layout_toLeftOf="@+id/editText1"
    安卓:text="Angle" />

 </RelativeLayout>

显示

<uses-sdk
    安卓:minSdkVersion="8"
    安卓:targetSdkVersion="16" />
<uses-permission 安卓:name="安卓.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission 安卓:name="安卓.permission.ACCESS_FINE_LOCATION"/>

<application
    安卓:allowBackup="true"
    安卓:icon="@drawable/ic_launcher"
    安卓:label="@string/app_name"
    安卓:theme="@style/AppTheme" >
    <activity
        安卓:name="com.example.sensormagnet.MainActivity"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />

            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

但它没有起作用

它返回了疯狂的值

我确信周围没有任何东西会影响磁场+

救救我!谢谢


共 (0) 个答案