-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arreglar juego con brújula #35
Comments
Pospuesto para una siguiente versión package com.martinlaizg.geofind.views.fragment.play;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.martinlaizg.geofind.R;
import butterknife.BindView;
import butterknife.ButterKnife;
public class PlayCompassFragment
extends PlayTourFragment
implements SensorEventListener {
private static final String TAG = PlayCompassFragment.class.getSimpleName();
private final float[] accelerometerReading = new float[3];
private final float[] magnetometerReading = new float[3];
@BindView(R.id.navigation_image)
ImageView navigation_image;
private SensorManager sensorManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_play_compass, container, false);
ButterKnife.bind(this, view);
sensorManager = (SensorManager) requireActivity().getSystemService(Context.SENSOR_SERVICE);
return view;
}
@Override
public void onSensorChanged(SensorEvent event) {
if(usrLocation != null && placeLocation != null) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
System.arraycopy(event.values, 0, accelerometerReading, 0,
accelerometerReading.length);
} else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values, 0, magnetometerReading, 0,
magnetometerReading.length);
}
// Rotation matrix based on current readings from accelerometer and magnetometer.
final float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerReading,
magnetometerReading);
// Express the updated rotation matrix as three orientation angles.
final float[] orientationAngles = new float[3];
SensorManager.getOrientation(rotationMatrix, orientationAngles);
Log.i(TAG, "rotation=" + orientationAngles[0]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// DO NOTHING
}
@Override
protected String TAG() {
return TAG;
}
@Override
public void onResume() {
super.onResume();
// Get updates from the accelerometer and magnetometer at a constant rate.
// To make batch operations more efficient and reduce power consumption,
// provide support for delaying updates to the application.
//
// In this example, the sensor reporting delay is small enough such that
// the application receives an update before the system checks the sensor
// readings again.
Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if(accelerometer != null) {
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL,
SensorManager.SENSOR_DELAY_UI);
}
Sensor magneticField = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if(magneticField != null) {
sensorManager.registerListener(this, magneticField, SensorManager.SENSOR_DELAY_NORMAL,
SensorManager.SENSOR_DELAY_UI);
}
}
@Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
void updateView() {
/*
In compass view we do not update the view with de location updates
We update the view with the onSensorChanged method
*/
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
La brújula sólo se actualiza cuándo se mueve el usuario.
Se debería de calcular la orientación del usuario en cada movimiento, pero después rotar la imagen a la vez que se rota el dispositivo móvil.
The text was updated successfully, but these errors were encountered: