Issue
I would like to smooth the accelerometer values a litle bit, because a little bit of shake is causing the canvas to vibrate.
I tried to add some delay but it didn't solve the problem.
How can I do that?
public BubblePage()
{
InitializeComponent();
Device.StartTimer(TimeSpan.FromSeconds(1f / 60), () =>
{
BubbleCenterCircle.InvalidateSurface();
return true;
});
}
void canvasView_BubbleCenterCircle(object sender, SKPaintSurfaceEventArgs e)
{
var surface = e.Surface;
var canvas = surface.Canvas;
canvas.Clear(SKColors.Transparent);
var width = e.Info.Width;
var height = e.Info.Height;
canvas.Translate(width / 2, height / 2);
canvas.Scale(width / 300f);
canvas.DrawLine(-60, 0, 60, 0, whiteLine);
canvas.DrawLine(0, -60, 0, 60, whiteLine);
canvas.DrawCircle(0, 0, 60f, blackPaint);
float x = (float)Math.Round(acceleration.X * RoundingValue, 1);
float y = (float)Math.Round(acceleration.Y * RoundingValue, 1);
canvas.DrawCircle(x, -y, 15f, whitePaint);
}
void Accelerometer_ReadingChanged(object sender, AccelerometerChangedEventArgs e)
{
acceleration = e.Reading.Acceleration;
}
Vector3 acceleration;
const float RoundingValue = 60f;
Solution
Sounds like you want to sample the acceleration to smooth it. Delaying it won't help in this case since the event will still be called.
If you have no control of how often the event is triggered, you can do something like this:
DateTime _lastRead;
TimeSpan _sampleRate = TimeSpan.FromSeconds(1);
void Accelerometer_ReadingChanged(object sender, AccelerometerChangedEventArgs e)
{
var now = DateTime.Now;
if ((now - _lastRead) >= _sampleRate)
{
acceleration = e.Reading.Acceleration;
_lastRead = now;
}
}
This won't assign the acceleration
value unless the difference between last read and now is equal or bigger than the sample rate.
If this is not what you wanted, you need to be more explicit in what it is you want to achieve.
Answered By - Cheesebaron
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.