- 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>



