Skip to content

Instantly share code, notes, and snippets.

@trcio
Created September 6, 2015 17:15
Show Gist options
  • Save trcio/03ba028a99182d1e3240 to your computer and use it in GitHub Desktop.
Save trcio/03ba028a99182d1e3240 to your computer and use it in GitHub Desktop.
Soundcloud music bars on the mobile app
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
public sealed class SoundcloudBars : Control
{
private readonly Timer AnimationTimer;
private readonly List<Bar> Bars;
private readonly float[] Increments = {.5f, 1f, .8f, .6f, .7f, 1f};
public SoundcloudBars()
{
DoubleBuffered = true;
Size = new Size(23, 23);
AnimationTimer = new Timer
{
Interval = 10
};
AnimationTimer.Elapsed += AnimationTick;
Bars = new List<Bar>();
ResetBars();
}
public void Start()
{
AnimationTimer.Start();
}
public void Stop()
{
AnimationTimer.Stop();
}
public void Reset()
{
ResetBars();
}
private void ResetBars()
{
Bars.Clear();
for (var i = 0; i < 6; i++)
{
Bars.Add(new Bar
{
GoingUp = true,
TickIncrement = Increments[i],
Bounds = new RectangleF(i * 4, 0, 2, 20)
});
}
}
private void AnimationTick(object sender, System.Timers.ElapsedEventArgs e)
{
foreach (var bar in Bars)
{
var increment = bar.TickIncrement*(bar.GoingUp ? -1 : 1);
bar.Bounds = new RectangleF(bar.Bounds.X, bar.Bounds.Y + increment, bar.Bounds.Width, bar.Bounds.Height - increment);
if (bar.Bounds.Y > 13)
bar.GoingUp = true;
else if (bar.Bounds.Y < 0)
bar.GoingUp = false;
}
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
g.Clear(BackColor);
foreach (var bar in Bars)
g.FillRectangle(Brushes.WhiteSmoke, bar.Bounds);
}
}
public class Bar
{
public RectangleF Bounds { get; set; }
public float TickIncrement { get; set; }
public bool GoingUp { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment