Giter VIP home page Giter VIP logo

Comments (2)

martinlaizg avatar martinlaizg commented on August 29, 2024

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
		 */
	}
}

from tfg-geo-find-android.

martinlaizg avatar martinlaizg commented on August 29, 2024

fix-compass-tour

from tfg-geo-find-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.