Gsensor_方向感应实现
<h1>Gsensor_指南针实现</h1>
<p>SensorManager .getDefaultSensor(Sensor.TYPE_ORIENTATION); 这个方法其实是已经被Android抛弃的方法,用下面两个来代替实现
//<a href="http://blog.csdn.net/genius9_9/article/details/44035955">http://blog.csdn.net/genius9_9/article/details/44035955</a> 参考文章</p>
<pre><code>public class MainActivity extends Activity {
private static final String TAG = &quot;gsen&quot;;
ImageView imgView;
SensorManager manager;
private float targetDegree =0.0f;
private float[] mMageneticValues = new float[3];
private float[] mAcceleValues = new float[3];
private TextView azimuthAngle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView = (ImageView) findViewById(R.id.img);
azimuthAngle = (TextView) findViewById(R.id.azimuth_angle_value) ;
imgView.setKeepScreenOn(true);
manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
//SensorManager .getDefaultSensor(Sensor.TYPE_ORIENTATION); 这个方法其实是已经被Android抛弃的方法,用下面两个来代替实现
//http://blog.csdn.net/genius9_9/article/details/44035955 参考文章
Sensor mAcceleSensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); // 加速度传感器 :Sensor.TYPE_ACCELEROMETER
Sensor mMagneticSensor = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); //磁场强度传感器:Sensor.TYPE_MAGNETIC_FIELD
manager.registerListener(mOrientationSensorEventListener, mAcceleSensor,
SensorManager.SENSOR_DELAY_NORMAL);
manager.registerListener(mOrientationSensorEventListener, mMagneticSensor,
SensorManager.SENSOR_DELAY_NORMAL);
List&lt;Sensor&gt; deviceSensors = manager.getSensorList(Sensor.TYPE_ALL);
for (int i = 0; i &lt; deviceSensors.size(); i++) {
Log.d(&quot;gsen&quot;, &quot; deviceSensors = &quot;+ deviceSensors.get(i).getName()) ;
}
super.onResume();
}
SensorEventListener mOrientationSensorEventListener = new SensorEventListener() {
private float predegree = 0;
public void onSensorChanged(SensorEvent event) {
int sensorType = event.sensor.getType();
Log.d(TAG,&quot; onSensorChanged() sensorType = &quot;+sensorType);
if(sensorType == Sensor.TYPE_ACCELEROMETER){ //加速度传感器
mAcceleValues = event.values;
}
if(sensorType == Sensor.TYPE_MAGNETIC_FIELD){ // 磁场强度传感器
mMageneticValues = event.values;
}
calculateOrientation();
/*******************************test**********************************/
/* float degree = event.values[0];// 数组中的第一个数是方向值
RotateAnimation anim = new RotateAnimation(predegree, -degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(200);
// imgView.setAnimation(anim);//这句错误
imgView.startAnimation(anim);
predegree = -degree;//记录这一次的起始角度作为下次旋转的初始角度
Log.d(&quot;gsen&quot;, &quot;predegree = &quot;+ predegree) ;*/
/**
float x=event.values[SensorManager.DATA_X];
float y=event.values[SensorManager.DATA_Y];
float z=event.values[SensorManager.DATA_Z];
Log.i(&quot;XYZ&quot;, &quot;x=&quot;+(int)x+&quot;,y=&quot;+(int)y+&quot;,z=&quot;+(int)z);
*/
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(TAG,&quot; onAccuracyChanged()&quot;);
}
};
@Override
protected void onPause() {
super.onPause();
manager.unregisterListener(mOrientationSensorEventListener);
}
// 计算方向
private void calculateOrientation() {
float[] values = new float[3];
float[] R = new float[9];
manager.getRotationMatrix(R, null, mAcceleValues, mMageneticValues);
manager.getOrientation(R, values);
values[0] = (float) Math.toDegrees(values[0]);
targetDegree = (-values[0]+360.0f) % 360;
Log.d(TAG,&quot; mAcceleValues[0]=&quot;+mAcceleValues[0] +&quot;, mMageneticValues[0] ==&quot; +mMageneticValues[0] );
Log.d(TAG,&quot; calculateOrientation() values[0]=&quot;+values[0] );
if (values[0] &gt;= -5 &amp;&amp; values[0] &lt; 5) {
azimuthAngle.setText(&quot;正北&quot;);
} else if (values[0] &gt;= 5 &amp;&amp; values[0] &lt; 85) {
// Log.i(TAG, &quot;东北&quot;);
azimuthAngle.setText(&quot;东北&quot;);
} else if (values[0] &gt;= 85 &amp;&amp; values[0] &lt;= 95) {
// Log.i(TAG, &quot;正东&quot;);
azimuthAngle.setText(&quot;正东&quot;);
} else if (values[0] &gt;= 95 &amp;&amp; values[0] &lt; 175) {
// Log.i(TAG, &quot;东南&quot;);
azimuthAngle.setText(&quot;东南&quot;);
} else if ((values[0] &gt;= 175 &amp;&amp; values[0] &lt;= 180)
|| (values[0]) &gt;= -180 &amp;&amp; values[0] &lt; -175) {
// Log.i(TAG, &quot;正南&quot;);
azimuthAngle.setText(&quot;正南&quot;);
} else if (values[0] &gt;= -175 &amp;&amp; values[0] &lt; -95) {
// Log.i(TAG, &quot;西南&quot;);
azimuthAngle.setText(&quot;西南&quot;);
} else if (values[0] &gt;= -95 &amp;&amp; values[0] &lt; -85) {
// Log.i(TAG, &quot;正西&quot;);
azimuthAngle.setText(&quot;正西&quot;);
} else if (values[0] &gt;= -85 &amp;&amp; values[0] &lt; -5) {
// Log.i(TAG, &quot;西北&quot;);
azimuthAngle.setText(&quot;西北&quot;);
}
}
}
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;fill_parent&quot;
android:orientation=&quot;vertical&quot;
android:background=&quot;@android:color/white&quot;
android:gravity=&quot;center&quot; &gt;
&lt;ImageView
android:id=&quot;@+id/img&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:src=&quot;@drawable/sensorimg&quot;/&gt;
&lt;TextView android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:textSize=&quot;20sp&quot;
android:textColor=&quot;#00aadd&quot;
android:gravity=&quot;center&quot;
android:id=&quot;@+id/azimuth_angle_value&quot;/&gt;
&lt;/LinearLayout&gt;</code></pre>