asked on
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);
}
}
ASKER
ASKER
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);
sensorTesty.x_ax = x;
sensorTesty.y_ax = y;
this.r = r;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(sensorTesty.x_ax, sensorTesty.y_ax, r,mPaint);
}
}
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;
public static float x_ax = 90;
public static 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;
FrameLayout main;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
main = (FrameLayout) findViewById(R.id.main_view);
main.addView(new Ball(this,x_ax,y_ax,25));
x_ax = 300;
y_ax = 300;
main.invalidate();
}
// 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;
}
*/
main.invalidate();
//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
}
}
ASKER
ASKER
ASKER
Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets. Android's user interface is based on direct manipulation, using touch gestures that loosely correspond to real-world actions, such as swiping, tapping and pinching, to manipulate on-screen objects, along with a virtual keyboard for text input. In addition to touchscreen devices, variants of Android are also used on notebooks, game consoles, digital cameras, televisions, automobiles and other electronics. Applications are usually developed in Java programming language using the Android software development kit (SDK), but other development environments are also available, including Delphi, Ruby and Visual Studio (using C++).
TRUSTED BY
ASKER
can you take a look at the adjusted code please
Open in new window