MainActivity

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
private ImageView ivDinamic;
private TextView tvDegree;
private float current_degree = 0f;
private SensorManager sensorManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
ivDinamic = findViewById(R.id.ivDinamic);
tvDegree = findViewById(R.id.tvDegree);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

}

@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);

}

@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
float degree = Math.round(event.values[0]);

tvDegree.setText("Degree from North: " + Float.toString(degree) + " degrees");


RotateAnimation ra = new RotateAnimation(current_degree, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

ra.setDuration(210);

ra.setFillAfter(true);

ivDinamic.startAnimation(ra);
current_degree = -degree;

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}

Colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#0A0000</color>
<color name="colorPrimaryDark">#000003</color>
<color name="colorAccent">#D81B60</color>
<color name="black">#000000</color>
<color name="white">#FFFFFF</color>
</resources>