Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. package com.example.kamil.minicredo;
  2.  
  3. import android.content.Context;
  4. import android.hardware.Sensor;
  5. import android.hardware.SensorEvent;
  6. import android.hardware.SensorEventListener;
  7. import android.hardware.SensorManager;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.TextView;
  12.  
  13. import static android.R.attr.gravity;
  14.  
  15. public class MainActivity extends AppCompatActivity implements SensorEventListener {
  16. private TextView textX,textY,textZ,isLying;
  17. private Sensor mySensor;
  18. private SensorManager SM;
  19.  
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25.  
  26. SM=(SensorManager)getSystemService(SENSOR_SERVICE);
  27. mySensor=SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  28. SM.registerListener(this,mySensor,SensorManager.SENSOR_DELAY_NORMAL);
  29. textX=(TextView)findViewById(R.id.xView);
  30. textY=(TextView)findViewById(R.id.yView);
  31. textZ=(TextView)findViewById(R.id.zView);
  32. isLying=(TextView)findViewById(R.id.IsLying);
  33.  
  34.  
  35.  
  36.  
  37. }
  38.  
  39. @Override
  40. public void onSensorChanged(SensorEvent event) {
  41.  
  42. float[] values = event.values;
  43. // Movement
  44. float x = values[0];
  45. float y = values[1];
  46. float z = values[2];
  47. float norm_Of_g =(float) Math.sqrt(x * x + y * y + z * z);
  48. textX.setText("y"+event.values[0]);
  49. textY.setText("y" + event.values[1]);
  50. textZ.setText("z" + event.values[2]);
  51. // Normalize the accelerometer vector
  52. x = (x / norm_Of_g);
  53. y = (y / norm_Of_g);
  54. z = (z / norm_Of_g);
  55. int inclination = (int) Math.round(Math.toDegrees(Math.acos(z)));
  56. if (!(inclination < 25 || inclination > 155)) {
  57. isLying.setVisibility(View.INVISIBLE);///to jeszcze nie dziala tak jak powinno xD
  58.  
  59. }
  60. }
  61.  
  62. @Override
  63. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  64. //
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement