Android - How to "Shake to Erase"?

This may be a simple question, but I'm stuck. I am trying to implement the "shake to erase" function in a drawing program (a simple drawing application). I can not make it work. Here is 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) { } }; 

SensorEventListener is based on this example . I do this in an if statement, but the canvas will not be reset until I touch the screen (new touch event).

I want the canvas to be reset / erase during the shake event, without any additional requests from the user.

Any help would be great, thanks!

+4
source share
1 answer

You may need to call invalidate on the graphic so that it can be redrawn. Hope this helps! http://developer.android.com/guide/topics/graphics/index.html

+2
source

Source: https://habr.com/ru/post/1340489/


All Articles