Hi,

You need to account for Gravity, see
http://developer.android.com/reference/android/hardware/SensorEvent.html

Regards

Sensor.TYPE_ACCELEROMETER:

All values are in SI units (m/s^2)
values[0]: Acceleration minus Gx on the x-axis
values[1]: Acceleration minus Gy on the y-axis
values[2]: Acceleration minus Gz on the z-axis
A sensor of this type measures the acceleration applied to the device
(Ad). Conceptually, it does so by measuring forces applied to the
sensor itself (Fs) using the relation:
Ad = - ∑Fs / mass
In particular, the force of gravity is always influencing the measured
acceleration:
Ad = -g - ∑F / mass
For this reason, when the device is sitting on a table (and obviously
not accelerating), the accelerometer reads a magnitude of g = 9.81 m/
s^2
Similarly, when the device is in free-fall and therefore dangerously
accelerating towards to ground at 9.81 m/s^2, its accelerometer reads
a magnitude of 0 m/s^2.
It should be apparent that in order to measure the real acceleration
of the device, the contribution of the force of gravity must be
eliminated. This can be achieved by applying a high-pass filter.
Conversely, a low-pass filter can be used to isolate the force of
gravity.

     public void onSensorChanged(SensorEvent event)
     {
          // alpha is calculated as t / (t + dT)
          // with t, the low-pass filter's time-constant
          // and dT, the event delivery rate

          final float alpha = 0.8;

          gravity[0] = alpha * gravity[0] + (1 - alpha) *
event.values[0];
          gravity[1] = alpha * gravity[1] + (1 - alpha) *
event.values[1];
          gravity[2] = alpha * gravity[2] + (1 - alpha) *
event.values[2];

          linear_acceleration[0] = event.values[0] - gravity[0];
          linear_acceleration[1] = event.values[1] - gravity[1];
          linear_acceleration[2] = event.values[2] - gravity[2];
     }

Examples:
When the device lies flat on a table and is pushed on its left side
toward the right, the x acceleration value is positive.
When the device lies flat on a table, the acceleration value is +9.81,
which correspond to the acceleration of the device (0 m/s^2) minus the
force of gravity (-9.81 m/s^2).
When the device lies flat on a table and is pushed toward the sky with
an acceleration of A m/s^2, the acceleration value is equal to A+9.81
which correspond to the acceleration of the device (+A m/s^2) minus
the force of gravity (-9.81 m/s^2).

On Jul 21, 4:20 pm, sandeep agrawal <[email protected]> wrote:
> Hi All,
>
> I am developing one application in which i want to paint using
> accelerometer sensor, this is my code,
>
> this is my drawmainactivity
>
> package com.drawline;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.view.Window;
> import android.view.WindowManager;
>
> public class DrawMainActivity extends Activity {
>
>         DrawlineActivity drawView;
>             @Override
>         protected void onCreate(Bundle savedInstanceState) {
>                 // TODO Auto-generated method stub
>                 super.onCreate(savedInstanceState);
>
>         // Set full screen view
>
> getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
>
> WindowManager.LayoutParams.FLAG_FULLSCREEN);
>         requestWindowFeature(Window.FEATURE_NO_TITLE);
>
>         drawView = new DrawlineActivity(this);
>         setContentView(drawView);
>         drawView.requestFocus();
>             }
>
>         }
>
> This is the code in which i am trying to paint
>
> package com.drawline;
>
> import java.util.ArrayList;
> import java.util.List;
>
> import android.app.Activity;
> import android.content.Context;
> import android.graphics.Canvas;
> import android.graphics.Color;
> import android.graphics.Paint;
> import android.graphics.drawable.shapes.ArcShape;
> import android.hardware.Sensor;
> import android.hardware.SensorEvent;
> import android.hardware.SensorEventListener;
> import android.hardware.SensorManager;
> import android.os.Bundle;
> import android.util.Log;
> import android.view.MotionEvent;
> import android.view.View;
> import android.view.View.OnTouchListener;
> import android.widget.LinearLayout;
>
> public class DrawlineActivity extends View implements
> SensorEventListener {
>         private static final String TAG = "DrawView";
> float startx=40,starty=40,oldx,oldy,x,y;
>     List<Point> points = new ArrayList<Point>();
>     Paint paint = new Paint();
> SensorManager mSensorManager;
>     public DrawlineActivity(Context context) {
>         super(context);
>         setFocusable(true);
>         setFocusableInTouchMode(true);
>
>         mSensorManager =
> (SensorManager)context.getSystemService(context.SENSOR_SERVICE);
> mSensorManager.registerListener(this,
>
> mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
>                SensorManager.SENSOR_DELAY_NORMAL);
>         paint.setColor(Color.WHITE);
>         paint.setAntiAlias(true);
>     }
>
>     @Override
>     public void onDraw(Canvas canvas) {
>         for (Point point : points) {
>             canvas.drawCircle(point.x+100,  point.y +100, 5, paint);
>
>             //canvas.drawLine(startx,starty, startx,starty, paint);
>             // Log.d(TAG, "Painting: "+point);
>             Log.d("SANDEEP", "Inside ondraw " + point.x+ point.y);
>         }
>     }
>
> //    public boolean onTouch(View view, MotionEvent event) {
> //        // if(event.getAction() != MotionEvent.ACTION_DOWN)
> //        // return super.onTouchEvent(event);
> //        Point point = new Point();
> //        point.x = event.getX();
> //        point.y = event.getY();
> //        points.add(point);
> //        invalidate();
> //        Log.d(TAG, "point: " + point);
> //        return true;
> //    }
>
>         @Override
>         public void onAccuracyChanged(Sensor sensor, int accuracy) {
>                 // TODO Auto-generated method stub
>
>         }
>
>         @Override
>         public void onSensorChanged(SensorEvent event) {
>                 // TODO Auto-generated method stub
>                 Point point = new Point();
>
>                 point.x= event.values[0] *5;
>         point.y  = event.values[1]*5;
>
>      //   point.x=(int) (x + point.x);
>        //  point.y= (int) (y+ point.y);
>         points.add(point);
>         invalidate();
>         Log.d(TAG, "point: " + startx + starty);
>       //  startx= startx + oldx;
>       //  starty = starty + oldy;
>         oldx = startx;
>     oldy = starty ;
>         }
>         }
>
> class Point {
>     float x=40, y=40;
>
>     @Override
>     public String toString() {
>         return x + ", " + y;
>     }
>
> }
>
> Can anyone of you please correct it what i am doing wrong.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to