package org.example.sensortesty;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
public class sensorTesty extends Activity implements SensorEventListener {
public float x ;
public float y ;
public float z;
public int r;
float sensX = 0;
float sensY = 0;
float sensZ = 0;
float x_ax = 90;
float y_ax = 90;
/** Called when the activity is first created. */
SensorManager sensorManager = null;
// for accelerometer values
TextView outputX;
TextView outputY;
TextView outputZ;
// for orientation values
TextView outputX2;
TextView outputY2;
TextView outputZ2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
// main.addView(new Ball(this,50,50,25));
}
// just some for data output
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_ORIENTATION);
}
@Override
protected void onStop() {
super.onStop();
sensorManager.unregisterListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
sensorManager.unregisterListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
}
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
outputX = (TextView) findViewById(R.id.TextView01);
outputY = (TextView) findViewById(R.id.TextView02);
outputZ = (TextView) findViewById(R.id.TextView03);
outputX2 = (TextView) findViewById(R.id.TextView04);
outputY2 = (TextView) findViewById(R.id.TextView05);
outputZ2 = (TextView) findViewById(R.id.TextView06);
x = event.values[0];
y = event.values[1];
z = event.values[2];
if(z>2){
outputX.setText("Face Up");
}else if(z<-2){
outputX.setText("Face Down");
}
if(x>1){
outputX.setText("Left Side");
if(x_ax > 20)
x_ax = x_ax - 10;
}else if(x<-1){
outputX.setText("Right Side");
if(x_ax < 300)
x_ax = x_ax + 10;
}
if(y>1){
outputX.setText("Standing Up");
if(y_ax < 370)
y_ax = y_ax + 10;
}
else if(y<-1){
outputX.setText("On Head");
if(y_ax > 20)
y_ax = y_ax - 10;
}else{
outputX.setText("In Between");
}
/*
if( event.values[0] > 9)
{
sensX = event.values[0];
x = x + 10;
}
else if(event.values[0] < -9)
{
sensX = event.values[0];
x = x - 10;
}
if(event.values[1] > 9)
{
sensY = event.values[1];
y = y + 10;
}
else if( event.values[1] < -9)
{
sensY = event.values[1];
y = y - 10;
}
if(sensZ > event.values[2])
{
sensZ = event.values[2];
z = z + 10;
}
else if(sensZ < event.values[2])
{
sensZ = event.values[2];
z = z - 10;
}
*/
FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
main.removeAllViews();
main.addView(new Ball(this,x_ax,y_ax,25));
//circle.moveTo(x, y);
//main.addView(new Ball(this,40,40,25));
break;
case Sensor.TYPE_ORIENTATION:
//x = event.values[0];
// y = event.values[1];
//circle.moveTo(x, y);
//outputX2.setText("x:" + Float.toString(event.values[0]));
//outputY2.setText("y:" + Float.toString(event.values[1]));
//outputZ2.setText("z:" + Float.toString(event.values[2]));
break;
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
package org.example.sensortesty;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class Ball extends View {
private float x;
private float y;
private int r;
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Ball(Context context, float x, float y, int r) {
super(context);
mPaint.setColor(0xFFFF0000);
this.x = x;
this.y = y;
this.r = r;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r,mPaint);
}
}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE