Skip to content
Snippets Groups Projects
Commit 5f56ebed authored by SCHILLING Juliette's avatar SCHILLING Juliette
Browse files

affichage avec orientation ok

parent 961b25c3
Branches
No related tags found
No related merge requests found
package com.example.myhabitat;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
......@@ -19,11 +25,16 @@ import habitat.Piece;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.time.Duration;
public class ModeImmersionActivity extends AppCompatActivity {
public class ModeImmersionActivity extends AppCompatActivity implements SensorEventListener {
Habitat habitat;
Mur murEnCours;
Piece pieceEnCours;
private SensorManager sensorManager;
private ImageView imageViewBoussole;
private float debut = 0f;
private ImageView imageViewMur;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -40,6 +51,15 @@ public class ModeImmersionActivity extends AppCompatActivity {
}
setContentView(R.layout.activity_mode_immersion);
pieceEnCours = habitat.getPieces().get(0);
affichePiece(pieceEnCours);
imageViewBoussole = findViewById(R.id.imageViewBoussole);
imageViewMur = findViewById(R.id.imageViewMur);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
public boolean onCreateOptionsMenu(Menu menu) {
......@@ -85,8 +105,6 @@ public class ModeImmersionActivity extends AppCompatActivity {
}
public void afficheMur(){
ImageView imageViewMur = findViewById(R.id.imageViewMur);
//On récupère la photo
FileInputStream fis = null;
try {
......@@ -122,4 +140,66 @@ public class ModeImmersionActivity extends AppCompatActivity {
murEnCours = pieceEnCours.getMurOrientation(Orientation.OUEST);
afficheMur();
}
@Override
public void onSensorChanged(SensorEvent event) {
// On récupère l'angle
float angle = -(Math.round(event.values[0]));
//On créé l'animation de rotation
RotateAnimation rotateAnimation = new RotateAnimation(debut, angle, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(200);
//Et on la lance
if(imageViewBoussole == null) {
imageViewBoussole = findViewById(R.id.imageViewBoussole);
}
imageViewBoussole.startAnimation(rotateAnimation);
if(angle<(-45) && angle>=(-135)){
murEnCours = pieceEnCours.getMurOrientation(Orientation.EST);
} else if (angle<(-135) && angle>=(-225)) {
murEnCours = pieceEnCours.getMurOrientation(Orientation.SUD);
} else if (angle<(-225) && angle>=(-315)) {
murEnCours = pieceEnCours.getMurOrientation(Orientation.OUEST);
}else{
murEnCours = pieceEnCours.getMurOrientation(Orientation.NORD);
}
afficheMur();
Toast.makeText(getBaseContext(), murEnCours.getOrientation().toString(), Toast.LENGTH_SHORT).show();
//Maj de l'angle de depart
debut = angle;
Log.i("testRotationBoussole", "rotation = " + angle);
/*
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
*/
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onPause() {
sensorManager.unregisterListener(this);
super.onPause();
}
@Override
protected void onResume() {
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
}
\ No newline at end of file
package outils;
public class GestionnaireBoussole {
public GestionnaireBoussole() {
}
}
app/src/main/res/drawable/boussole.png

170 KiB

......@@ -65,4 +65,12 @@
app:layout_constraintVertical_bias="0.863"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:layout_width="154dp"
android:layout_height="125dp"
app:srcCompat="@drawable/boussole"
android:id="@+id/imageViewBoussole"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment