0

I'm attempting to build a bubble level application but am having issues with the bubble coming back to center when phone is on a flat surface. I believe something needs to be added to the onPause method. However, simply entering initial coordinates doesn't seem to work. Even if it worked, position change should be gradual but I'm uncertain of how to do that. Any help would be greatly appreciated.

Here is the code:

public class ActivityLevel extends AppCompatActivity implements SensorEventListener {

private Sensor accelerometer;
private SensorManager accelerometerManager;
private AnimatedView animatedView = null;

@Override
protected void onCreate (Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tools_level);
    FrameLayout level = findViewById(R.id.level);
    //Hide Action Bar
    if (getSupportActionBar() != null) {
        getSupportActionBar().hide();
    }
    accelerometerManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    accelerometer = accelerometerManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    accelerometerManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_GAME);
    //Define animated view and add view to existing layout
    animatedView = new AnimatedView(this);
    level.addView(animatedView);
}
@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
        animatedView.onSensorEvent(event);
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@Override
protected void onPause() {
    super.onPause();
    accelerometerManager.unregisterListener(this);

}

@Override
protected void onResume() {
    super.onResume();
    accelerometerManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_GAME);
}

public static class AnimatedView extends View{

    private static final int CIRCLE_RADIUS = 50;
    private final Paint paint;

    //Set Initial Coordinates
    private int x = getResources().getDisplayMetrics().widthPixels /2;
    private int y = getResources().getDisplayMetrics().heightPixels /2;
    private int height;
    private int width;


    public AnimatedView(Context context) {
        super(context);
        //Bubble
        paint = new Paint();
        paint.setColor(Color.GREEN);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        height = h;
        width = w;

    }
    //Change coordinates when accelerometer is active
    public void onSensorEvent (SensorEvent event) {
        x = x - (int) event.values[0];
        y = y + (int) event.values[1];

        //bounds
        if (x <= CIRCLE_RADIUS){
            x = CIRCLE_RADIUS;
        }
        if (x >= width - CIRCLE_RADIUS){
            x = width - CIRCLE_RADIUS;
        }
        if (y <= CIRCLE_RADIUS){
            y = CIRCLE_RADIUS;
        }
        if (y >= height - CIRCLE_RADIUS){
            y = height - CIRCLE_RADIUS;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //Draw a circle
        canvas.drawCircle(x,y,CIRCLE_RADIUS,paint);
        invalidate();
    }
}
}
RUSKIED
  • 39
  • 1
  • 2
  • 8

0 Answers0