troubleshooting Question

moving ball using ACCELEROMETER sensor is lagging...why?

Avatar of logicallayer
logicallayer asked on
Android
9 Comments2 Solutions3364 ViewsLast Modified:
Hi,

I'm somehow new to android as I'm trying to make a very simple game that relys on moving the ball or circle based on the tilting of the cell phone, the movement works good maybe there is a room for improvement to get better accuracy but doesn't really concerns me for now

my problem is that the circle lags while it's moving, strangely I know I played alot of games before that are similar on my phone and it suppose to be able to handle such a simple game

I would guess that my problem is the way I'm listening to the sensor that is causing the lag but I don't know how to fix, can someone give some hints please?

there are some extra texts in the screen as I was doing some testing with them feel free to ignore them...
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

	}
	
}

Open in new window

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);	
		}
		
}

Open in new window

ASKER CERTIFIED SOLUTION
Francisco Igor
Software Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros