Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     public void onSensorChanged(SensorEvent event) {
  3.         int sensorType = event.sensor.getType();
  4.         switch (sensorType){
  5.             case Sensor.TYPE_ROTATION_VECTOR:
  6.  
  7.                 SensorManager.getRotationMatrixFromVector(rMat,event.values);
  8.                 compass = Math.round( (int) (Math.toDegrees(SensorManager.getOrientation(rMat,orientation)[0]) + 360) % 360);
  9.  
  10.                 if (initialCompass < 0){
  11.                     initialCompass = compass;
  12.                 }
  13.  
  14.                 compassDiff = compass - initialCompass;
  15.                 System.out.println(compassDiff);
  16.  
  17.                 Log.d(TAG, "Compassdiff: "+ compassDiff);
  18.                 Log.d(TAG, "InitialCompass: "+ initialCompass);
  19.                 Log.d(TAG, "Compass: "+ compass);
  20.  
  21.                 // Down
  22.                 if (inRange(compassDiff, 137, 223) || inRange(compassDiff, -223, -137)){
  23.                     final Animation animation = createAnimation();
  24.                     final ImageButton btnDown = findViewById(R.id.imageDown);
  25.                     btnDown.startAnimation(animation);
  26.                     btnDown.setOnClickListener(new View.OnClickListener() {
  27.                         @Override
  28.                         public void onClick(final View view) {
  29.                             view.clearAnimation();
  30.                         }
  31.                     });
  32.  
  33.                     initialCompass += 180;
  34.                     if (initialCompass >= 360){
  35.                         initialCompass -= 360;
  36.                     }
  37.                 }
  38.  
  39.                 // Right
  40.                 else if (inRange(compassDiff, 47, 133) || inRange(compassDiff, -313, -227)){
  41.                     Log.d(TAG, "right entered");
  42.                     final Animation animation = createAnimation();
  43.                     final ImageButton btnRight = findViewById(R.id.imageRight);
  44.                     btnRight.startAnimation(animation);
  45.                     btnRight.setOnClickListener(new View.OnClickListener() {
  46.                         @Override
  47.                         public void onClick(final View view) {
  48.                             view.clearAnimation();
  49.                         }
  50.                     });
  51.  
  52.                     initialCompass += 90;
  53.                     if (initialCompass >= 360){
  54.                         initialCompass -= 360;
  55.                     }
  56.                 }
  57.  
  58.                 // Left
  59.                 else if (inRange(compassDiff, -133, -47) || inRange(compassDiff, 227, 313)){
  60.                     Log.d(TAG, "left entered");
  61.                     final Animation animation = createAnimation();
  62.                     final ImageButton btnLeft = findViewById(R.id.imageLeft);
  63.                     btnLeft.startAnimation(animation);
  64.                     btnLeft.setOnClickListener(new View.OnClickListener() {
  65.                         @Override
  66.                         public void onClick(final View view) {
  67.                             view.clearAnimation();
  68.                         }
  69.                     });
  70.  
  71.                     initialCompass -= 90;
  72.                     if (initialCompass < 0){
  73.                         initialCompass += 360;
  74.                     }
  75.                 }
  76.  
  77.                 // Up
  78.                 else if ((compassDiff >= -43) || (compassDiff <= 43)){
  79.                     Log.d(TAG, "up entered");
  80.                     final Animation animation = createAnimation();
  81.                     final ImageButton btnUp = findViewById(R.id.imageUp);
  82.                     btnUp.startAnimation(animation);
  83.                     btnUp.setOnClickListener(new View.OnClickListener() {
  84.                         @Override
  85.                         public void onClick(final View view) {
  86.                             view.clearAnimation();
  87.                         }
  88.                     });
  89.                 }
  90.  
  91.                 break;
  92.  
  93.                 default :
  94.                     break;
  95.         }
  96.     }
  97.  
  98.     private Animation createAnimation() {
  99.         // Change alpha from fully visible to invisible
  100.         final Animation animation = new AlphaAnimation(1, 0);
  101.         // duration is set to half a second
  102.         animation.setDuration(500);
  103.         // Do not alter animation rate
  104.         animation.setInterpolator(new LinearInterpolator());
  105.         // Repeat animation infinitely
  106.         animation.setRepeatCount(3);
  107.         // Reverse animation at the end so the button will fade back in
  108.         animation.setRepeatMode(Animation.REVERSE);
  109.         return animation;
  110.     }
  111.  
  112.     // checks if compass difference is in range of const1 and const2
  113.     private Boolean inRange(float x, float const1, float const2) {
  114.         return x >= const1 && x <= const2;
  115.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement