Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.kamil.minicredo;
- import android.content.Context;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- import static android.R.attr.gravity;
- public class MainActivity extends AppCompatActivity implements SensorEventListener {
- private TextView textX,textY,textZ,isLying;
- private Sensor mySensor;
- private SensorManager SM;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- SM=(SensorManager)getSystemService(SENSOR_SERVICE);
- mySensor=SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- SM.registerListener(this,mySensor,SensorManager.SENSOR_DELAY_NORMAL);
- textX=(TextView)findViewById(R.id.xView);
- textY=(TextView)findViewById(R.id.yView);
- textZ=(TextView)findViewById(R.id.zView);
- isLying=(TextView)findViewById(R.id.IsLying);
- }
- @Override
- public void onSensorChanged(SensorEvent event) {
- float[] values = event.values;
- // Movement
- float x = values[0];
- float y = values[1];
- float z = values[2];
- float norm_Of_g =(float) Math.sqrt(x * x + y * y + z * z);
- textX.setText("y"+event.values[0]);
- textY.setText("y" + event.values[1]);
- textZ.setText("z" + event.values[2]);
- // Normalize the accelerometer vector
- x = (x / norm_Of_g);
- y = (y / norm_Of_g);
- z = (z / norm_Of_g);
- int inclination = (int) Math.round(Math.toDegrees(Math.acos(z)));
- if (!(inclination < 25 || inclination > 155)) {
- isLying.setVisibility(View.INVISIBLE);///to jeszcze nie dziala tak jak powinno xD
- }
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- //
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement