我有一个与android中的指南针相关的项目。
它只显示手机相对于Z轴的角度如下(角度计算跟随度)
这是我的代码
MainActivity.java
package com.example.sensormagnet;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.widget.EditText;
import android.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();
}
}
图形.java
package com.example.sensormagnet;
import java.net.URL;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.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
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="23dp"
android:layout_marginTop="20dp"
android:layout_toLeftOf="@+id/editText1"
android:text="Angle" />
</RelativeLayout>
显现
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sensormagnet.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
但它没有用。
它返回了疯狂的值。
而且我确信周围没有任何东西会影响磁场。+
帮帮我!谢谢