Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void onSensorChanged(SensorEvent event) {
- int sensorType = event.sensor.getType();
- switch (sensorType){
- case Sensor.TYPE_ROTATION_VECTOR:
- SensorManager.getRotationMatrixFromVector(rMat,event.values);
- compass = Math.round( (int) (Math.toDegrees(SensorManager.getOrientation(rMat,orientation)[0]) + 360) % 360);
- if (initialCompass < 0){
- initialCompass = compass;
- }
- compassDiff = compass - initialCompass;
- System.out.println(compassDiff);
- Log.d(TAG, "Compassdiff: "+ compassDiff);
- Log.d(TAG, "InitialCompass: "+ initialCompass);
- Log.d(TAG, "Compass: "+ compass);
- // Down
- if (inRange(compassDiff, 137, 223) || inRange(compassDiff, -223, -137)){
- final Animation animation = createAnimation();
- final ImageButton btnDown = findViewById(R.id.imageDown);
- btnDown.startAnimation(animation);
- btnDown.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(final View view) {
- view.clearAnimation();
- }
- });
- initialCompass += 180;
- if (initialCompass >= 360){
- initialCompass -= 360;
- }
- }
- // Right
- else if (inRange(compassDiff, 47, 133) || inRange(compassDiff, -313, -227)){
- Log.d(TAG, "right entered");
- final Animation animation = createAnimation();
- final ImageButton btnRight = findViewById(R.id.imageRight);
- btnRight.startAnimation(animation);
- btnRight.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(final View view) {
- view.clearAnimation();
- }
- });
- initialCompass += 90;
- if (initialCompass >= 360){
- initialCompass -= 360;
- }
- }
- // Left
- else if (inRange(compassDiff, -133, -47) || inRange(compassDiff, 227, 313)){
- Log.d(TAG, "left entered");
- final Animation animation = createAnimation();
- final ImageButton btnLeft = findViewById(R.id.imageLeft);
- btnLeft.startAnimation(animation);
- btnLeft.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(final View view) {
- view.clearAnimation();
- }
- });
- initialCompass -= 90;
- if (initialCompass < 0){
- initialCompass += 360;
- }
- }
- // Up
- else if ((compassDiff >= -43) || (compassDiff <= 43)){
- Log.d(TAG, "up entered");
- final Animation animation = createAnimation();
- final ImageButton btnUp = findViewById(R.id.imageUp);
- btnUp.startAnimation(animation);
- btnUp.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(final View view) {
- view.clearAnimation();
- }
- });
- }
- break;
- default :
- break;
- }
- }
- private Animation createAnimation() {
- // Change alpha from fully visible to invisible
- final Animation animation = new AlphaAnimation(1, 0);
- // duration is set to half a second
- animation.setDuration(500);
- // Do not alter animation rate
- animation.setInterpolator(new LinearInterpolator());
- // Repeat animation infinitely
- animation.setRepeatCount(3);
- // Reverse animation at the end so the button will fade back in
- animation.setRepeatMode(Animation.REVERSE);
- return animation;
- }
- // checks if compass difference is in range of const1 and const2
- private Boolean inRange(float x, float const1, float const2) {
- return x >= const1 && x <= const2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement