Accelerometer demo

Quickly share your Android Code Snippets here...

Accelerometer demo

Postby ashwani » Tue Mar 15, 2011 4:36 am

hi, all i have created a app that utilizes accelerometer. A 2d image of plane twists left or right as you move your device left or right respectively. i haven't tested it on a real device but it works fine on emulator. some app grabs are attached below. your comments and suggestions are welcome. source code of this app is similar to my compass app http://www.anddev.org/post95045.html#p95045. Source code of accelerometer demo is:
Code: Select all
package com.example.acc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;;

public class Accelerometer_Demo extends Activity implements SensorEventListener {
    /** Called when the activity is first created. */
   
   private SensorManagerSimulator sensorManager;   
   boolean sensorrunning;
   AccelerometerView compass;   
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       Log.v("Status","1");
       setContentView(R.layout.main);
       Log.v("Status","2");
       compass= (AccelerometerView)findViewById(R.id.mycompassview);     
       Log.v("Status","3");
       sensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
       Log.v("Status","4");
       sensorManager.registerListener(this,
               sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
               SensorManagerSimulator.SENSOR_DELAY_NORMAL);
       Log.v("Status","5");
         sensorrunning=true;
       sensorManager.connectSimulator();
       Log.v("Status","6");
    }
   
    /** Register for the updates when Activity is in foreground */
    @Override
    protected void onResume() {
      super.onResume();
      sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManagerSimulator.SENSOR_DELAY_NORMAL);
      sensorrunning=true;
    }
   
    /** Stop the updates when Activity is paused */
    @Override
    protected void onPause() {
      super.onPause();
      sensorManager.unregisterListener(this);
    }   
    @Override
   public void onAccuracyChanged(Sensor sensor, int accuracy) {
      // TODO Auto-generated method stub     
   }
    @Override
   public void onSensorChanged(SensorEvent event) {
      // TODO Auto-generated method stub
            //myAzimuth = Math.round(event.values[0]);               
           compass.updateDirection(event.values[0],event.values[1],event.values[2]);
   } 
    @Override
    protected void onDestroy(){
      super.onDestroy();
      if(sensorrunning){
         sensorManager.unregisterListener(this);
      }
   }
}



code for accelerometer view is:
Code: Select all
package com.example.acc;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


public class AccelerometerView extends View{
   private float direction = 0;
   private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
   private boolean firstDraw;
   private Bitmap arrow;
   private int xpos;
   private int ypos;   
      
   public AccelerometerView(Context context) {
   super(context);
   // TODO Auto-generated constructor stub
   init();
   }

   public AccelerometerView(Context context, AttributeSet attrs) {
   super(context, attrs);
   // TODO Auto-generated constructor stub
   init();
   }

   public AccelerometerView(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   // TODO Auto-generated constructor stub
   init();
   }

   private void init(){

   paint.setStyle(Paint.Style.STROKE);
   paint.setStrokeWidth(5);
   paint.setColor(Color.RED);
   paint.setTextSize(30);   
   arrow = BitmapFactory
    .decodeResource(getResources(), R.drawable.plane);   
   xpos = arrow.getWidth()/2;
   ypos = arrow.getHeight()/2;
   /*mpaint.setStyle(Paint.Style.STROKE);
   mpaint.setStrokeWidth(5);
   mpaint.setColor(Color.RED);
   mpaint.setTextSize(30);*/

   firstDraw = true;
   }

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   // TODO Auto-generated method stub
   setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
   }

   @Override
   protected void onDraw(Canvas canvas) {
   // TODO Auto-generated method stub

   int cxCompass = getMeasuredWidth()/2;
   int cyCompass = getMeasuredHeight()/2;
   float radiusCompass;

   if(cxCompass > cyCompass){
    radiusCompass = (float) (cyCompass * 0.9);
   }
   else{
    radiusCompass = (float) (cxCompass * 0.9);
   }
   Log.v("Radius", String.valueOf(radiusCompass));
   //canvas.drawCircle(cxCompass, cyCompass, radiusCompass, mpaint);
   //canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);

   if(!firstDraw){
    /*canvas.drawLine(cxCompass, cyCompass,
      (float)(cxCompass + radiusCompass * Math.sin((double)(-direction) * 3.14/180)),
      (float)(cyCompass - radiusCompass * Math.cos((double)(-direction) * 3.14/180)),
      paint);*/
    /*canvas.drawLine((float)(cxCompass + 7 * Math.sin((double)(-(direction+90)) * 3.14/180)),
            (float)(cyCompass - 7 * Math.cos((double)(-(direction+90)) * 3.14/180)),
            (float)(cxCompass + radiusCompass * Math.sin((double)(-direction) * 3.14/180)),
            (float)(cyCompass - radiusCompass * Math.cos((double)(-direction) * 3.14/180)),
            paint);
    canvas.drawLine((float)(cxCompass + 7 * Math.sin((double)(-(direction-90)) * 3.14/180)),
            (float)(cyCompass - 7 * Math.cos((double)(-(direction-90)) * 3.14/180)),
            (float)(cxCompass + radiusCompass * Math.sin((double)(-direction) * 3.14/180)),
            (float)(cyCompass - radiusCompass * Math.cos((double)(-direction) * 3.14/180)),
            paint);   */
       Matrix transform = new Matrix();       
       transform.setTranslate(cxCompass-xpos, cyCompass-ypos);      
       transform.preRotate(direction*10,xpos, ypos);      
       canvas.drawBitmap(arrow, transform, null);
   
    //canvas.drawText(String.valueOf(direction), cxCompass, cyCompass, paint);
   }

   }

   public void updateDirection(float x, float y, float z)
   {
   firstDraw = false;
   direction = x;
   invalidate();
   }
}



code for main.xml file is:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   <TextView
       android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/hello"
    />
       <view
         class="com.example.acc.AccelerometerView"
         android:id="@+id/mycompassview"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:background="@drawable/bgsky"
      />            
</LinearLayout>
Have Fun... :)
Attachments
grab2.png
plane tilted left
grab2.png (116.45 KiB) Viewed 2171 times
grab1.png
plane flying straight
grab1.png (113.01 KiB) Viewed 2171 times
ashwani
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 10, 2011 12:43 pm

Top

Re: Accelerometer demo

Postby Mike.Adams » Wed Apr 06, 2011 5:04 pm

very good share thanks!
Hi I'm Mike. Checkout my source code, projects and work here and let me know what you think.

Feel free to also upload and submit your code/projects there for all to see and find!
Mike.Adams
Developer
Developer
 
Posts: 26
Joined: Mon Mar 07, 2011 9:19 pm
Location: New York

Re: Accelerometer demo

Postby ashwani » Mon Apr 11, 2011 3:58 am

thanks buddy, i want to implement this code into a flying simulation demo. What are the things i need to do to achieve this..
ashwani
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 10, 2011 12:43 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests