Это то, что я сделал, вы можете ссылаться на этот код.
при изменении датчика
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
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);
azimut = orientation[0]; // orientation contains:
// azimut, pitch and roll
rotateCompass(azimut);
}
}
}
Рассчитать пеленг
private void rotateCompass(final float azimut) {
float azimuth = (float) Math.round(Math.toDegrees(azimut));
Location currentLoc = new Location("");
currentLoc.setLatitude(curr_lat);
currentLoc.setLongitude(curr_long);
Location target = new Location("");
target.setLatitude(dest_lat);
target.setLongitude(dest_lng);
float bearing = currentLoc.bearingTo(target); // (it's already in degrees)
if (bearing < 0) {
bearing = bearing + 360;
}
float direction = (float) (bearing - azimuth);
// If the direction is smaller than 0, add 360 to get the rotation clockwise.
if (direction < 0) {
direction = direction + 360;
}
showToast("" + direction);
rotateImageView(imgCompass, R.drawable.pin_finder, direction);
}
Повернуть изображение в соответствии с направлением в градусах
private void rotateImageView(ImageView imageView, int drawable, float rotate) {
// Decode the drawable into a bitmap
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
drawable);
// Get the width/height of the drawable
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = bitmapOrg.getWidth(), height = bitmapOrg.getHeight();
// Initialize a new Matrix
Matrix matrix = new Matrix();
// Decide on how much to rotate
rotate = rotate % 360;
// Actually rotate the image
matrix.postRotate(rotate, width, height);
// recreate the new Bitmap via a couple conditions
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
// BitmapDrawable bmd = new BitmapDrawable( rotatedBitmap );
// imageView.setImageBitmap( rotatedBitmap );
imageView.setImageDrawable(new BitmapDrawable(getResources(),
rotatedBitmap));
imageView.setScaleType(ScaleType.CENTER);
}
person
Kishore Jethava
schedule
07.05.2015