5

This may be an easy question, but I'm stuck. I am trying to implement the "shake to erase" feature in a drawing program (simple paint app). I can't get it to work. Here's my code:

private final SensorEventListener mSensorListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent se) {
          float x = se.values[0];
          float y = se.values[1];
          float z = se.values[2];
          mAccelLast = mAccelCurrent;
          mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
          float delta = mAccelCurrent - mAccelLast;
          mAccel = mAccel * 0.9f + delta; // perform low-cut filter

          if (mAccel > 2) {    
              mView.onDraw(mCanvas);
              mCanvas.drawBitmap(cache, 0, 0, new Paint(Paint.DITHER_FLAG));
          }
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
      };

The SensorEventListener is based off of this example. I make it into the if statement, but the canvas won't reset until after I've touched the screen (a new touch event).

I'd like the canvas to reset/erase during the shake event, without any further prompts from the user necessary.

Any help would be wonderful, thank you!

Community
  • 1
  • 1
Allison
  • 591
  • 3
  • 15

1 Answers1

2

You might have to call invalidate on the graphics object to get it to redraw. Hope that helps! http://developer.android.com/guide/topics/graphics/index.html

Gary
  • 202
  • 3
  • 7
  • Thank you! I tried replacing `mView.onDraw(mCanvas);` with `mView.invalidate();` -- works perfectly now! – Allison Feb 19 '11 at 22:45