Created
September 6, 2015 17:15
-
-
Save trcio/03ba028a99182d1e3240 to your computer and use it in GitHub Desktop.
Soundcloud music bars on the mobile app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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