001.
package
com.iutinvg.compass;
002.
003.
004.
import
android.content.Context;
005.
import
android.hardware.Sensor;
006.
import
android.hardware.SensorEvent;
007.
import
android.hardware.SensorEventListener;
008.
import
android.hardware.SensorManager;
009.
import
android.util.Log;
010.
import
android.view.LayoutInflater;
011.
import
android.view.View;
012.
import
android.view.animation.Animation;
013.
import
android.view.animation.RotateAnimation;
014.
import
android.widget.ImageView;
015.
import
android.widget.TextView;
016.
017.
public
class
Compass
implements
SensorEventListener {
018.
private
static
final
String TAG =
"Compass"
;
019.
020.
021.
private
SensorManager sensorManager;
022.
private
Sensor gsensor;
023.
private
Sensor msensor;
024.
private
float
[] mGravity =
new
float
[
3
];
025.
private
float
[] mGeomagnetic =
new
float
[
3
];
026.
private
float
azimuth = 0f;
027.
private
float
currectAzimuth =
0
;
028.
029.
030.
public
ImageView arrowView =
null
;
031.
032.
TextView tv;
033.
public
Compass(Context context) {
034.
sensorManager = (SensorManager) context
035.
.getSystemService(android.content.Context.SENSOR_SERVICE);
036.
gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
037.
msensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
038.
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
039.
View v = inflater.inflate( R.layout.activity_compass,
null
);
040.
tv = (TextView )v.findViewById(R.id.textView1);
041.
042.
}
043.
044.
045.
public
void
start() {
046.
sensorManager.registerListener(
this
, gsensor,
047.
SensorManager.SENSOR_DELAY_GAME);
048.
sensorManager.registerListener(
this
, msensor,
049.
SensorManager.SENSOR_DELAY_GAME);
050.
}
051.
052.
public
void
stop() {
053.
sensorManager.unregisterListener(
this
);
054.
}
055.
056.
private
void
adjustArrow() {
057.
if
(arrowView ==
null
) {
058.
Log.i(TAG,
"arrow view is not set"
);
059.
return
;
060.
}
061.
062.
Log.i(TAG,
"will set rotation from "
+ currectAzimuth +
" to "
063.
+ azimuth);
064.
065.
Animation an =
new
RotateAnimation(-currectAzimuth, -azimuth,
066.
Animation.RELATIVE_TO_SELF,
0
.5f, Animation.RELATIVE_TO_SELF,
067.
0
.5f);
068.
currectAzimuth = azimuth;
069.
070.
an.setDuration(
500
);
071.
an.setRepeatCount(
0
);
072.
an.setFillAfter(
true
);
073.
074.
arrowView.startAnimation(an);
075.
}
076.
077.
@Override
078.
public
void
onSensorChanged(SensorEvent event) {
079.
080.
081.
final
float
alpha =
0
.97f;
082.
083.
synchronized
(
this
) {
084.
if
(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
085.
086.
mGravity[
0
] = alpha * mGravity[
0
] + (
1
- alpha)
087.
* event.values[
0
];
088.
mGravity[
1
] = alpha * mGravity[
1
] + (
1
- alpha)
089.
* event.values[
1
];
090.
mGravity[
2
] = alpha * mGravity[
2
] + (
1
- alpha)
091.
* event.values[
2
];
092.
093.
094.
095.
096.
}
097.
098.
if
(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
099.
100.
101.
mGeomagnetic[
0
] = alpha * mGeomagnetic[
0
] + (
1
- alpha)
102.
* event.values[
0
];
103.
mGeomagnetic[
1
] = alpha * mGeomagnetic[
1
] + (
1
- alpha)
104.
* event.values[
1
];
105.
mGeomagnetic[
2
] = alpha * mGeomagnetic[
2
] + (
1
- alpha)
106.
* event.values[
2
];
107.
108.
109.
}
110.
111.
float
R[] =
new
float
[
9
];
112.
float
I[] =
new
float
[
9
];
113.
boolean
success = SensorManager.getRotationMatrix(R, I, mGravity,
114.
mGeomagnetic);
115.
if
(success) {
116.
float
orientation[] =
new
float
[
3
];
117.
SensorManager.getOrientation(R, orientation);
118.
119.
azimuth = (
float
) Math.toDegrees(orientation[
0
]);
120.
azimuth = (azimuth +
360
) %
360
;
121.
122.
123.
124.
125.
adjustArrow();
126.
}
127.
}
128.
}
129.
130.
131.
@Override
132.
public
void
onAccuracyChanged(Sensor sensor,
int
accuracy) {
133.
134.
135.
}
136.
}