Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:47:44+00:00 2026-06-01T11:47:44+00:00

I’have written a simple code, that gets accelerometer&orientation meter and display some graphics based

  • 0

I’have written a simple code, that gets accelerometer&orientation meter and display some graphics based on values:

package pl.aadamczyk.enginesdriver;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class EnginesDriverActivity extends Activity implements SensorEventListener
{
    private SensorManager sensorManager;
    private Sensor accelerometer;
    private Sensor orientation;
    private Panel panel;

    public EnginesDriverActivity()
    {
        sensorManager = null;
        accelerometer = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        orientation = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

        panel = new Panel(this.getApplicationContext(), null);

        setContentView(panel);
    }

     protected void onResume()
     {
         super.onResume();
         sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
         sensorManager.registerListener(this, orientation, SensorManager.SENSOR_DELAY_NORMAL);
     }

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

    @Override
    public void onSensorChanged(SensorEvent se)
    {
        Sensor source = se.sensor;

        if (source == accelerometer)
        {
            System.out.println("Updating orientation: " + se.values[1]);
            panel.setOrientation(se.values);
        }
        else if (source == orientation)
        {
            System.out.println("Updating TYPE_ACCELEROMETER: " + se.values[1]);
            panel.setAccelerometer(se.values);
        }

        panel.postInvalidate();
    }

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

    }
}


package pl.aadamczyk.enginesdriver;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class Panel extends View
{
    private Paint paint;
    private float[] orientation;
    private float[] accelerometer;

    public Panel(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        orientation = new float[]{0, 0, 0};
        accelerometer = new float[]{0, 0, 0};
    }

    public void setOrientation(float[] _orientation)
    {
        orientation = _orientation;
    }

    public void setAccelerometer(float[] _accelerometer)
    {
        accelerometer = _accelerometer;
    }   

    @Override
    protected void onDraw(Canvas canvas)
    {
        // Some calculations here
        float y = orientation[1];
        float z = orientation[2];

        System.out.println(y);

        // Some graphics here
    }
}

The problem is that it’s mixing orientation’s values with accelerometer’s, displaying weird things. There are two System.out.println() in the code – first one in onSensorChanged display good values, but second one in onDraw display orientation or accelerometer value.

How is it possible?

Thanks in advice and sorry for my english.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:47:45+00:00Added an answer on June 1, 2026 at 11:47 am

    my guess is that SensorEvent objects get reused by the system. So simply copy the values inOnSensorChanged or your setter methods inside the PanelView

            System.arraycopy(ev.values, 0,copy, 0, 3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.