Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Last active November 29, 2015 02:01
Show Gist options
  • Save Cheesebaron/650011d9a7ceec6fc5e7 to your computer and use it in GitHub Desktop.
Save Cheesebaron/650011d9a7ceec6fc5e7 to your computer and use it in GitHub Desktop.
Waves represented by circles! See gfycat here: http://gfycat.com/AfraidRequiredEsok
using System;
using Android.Content;
using Android.Graphics;
using Android.Runtime;
using Android.Util;
using Android.Views;
namespace CircleWaves
{
public class CircleView : View
{
private Paint _paint;
private Color _color;
public CircleView(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) { }
public CircleView(Context context)
: this(context, null) { }
public CircleView(Context context, IAttributeSet attrs)
: this(context, attrs, 0) { }
public CircleView(Context context, IAttributeSet attrs, int defStyleAttr)
: base(context, attrs, defStyleAttr)
{
Init();
}
public int Angle { get; set; }
public bool ClockWise { get; set; }
public float CircleRadius { get; set; }
public int Increment { get; set; }
public Color Color
{
get { return _color; }
set
{
_paint.Color = _color = value;
}
}
private bool _first;
private void Init()
{
_paint = new Paint(PaintFlags.AntiAlias);
_color = Color.Black;
_paint.StrokeWidth = 2;
_paint.Color = _color;
Angle = 360;
ClockWise = true;
_first = true;
}
public override void Draw(Canvas canvas)
{
base.Draw(canvas);
var width = Width - PaddingLeft - PaddingRight;
var radius = width / 2f;
_paint.SetStyle(Paint.Style.Stroke);
canvas.DrawCircle(Width / 2f, Height / 2f, radius, _paint);
if (!_first) {
if (ClockWise)
{
if (Angle == 360 || Angle > 360 || Angle < 0)
Angle = Increment;
else
Angle += Increment;
}
else
{
if (Angle == 1 || Angle > 360 || Angle < 0)
Angle = 360;
else
Angle -= Increment;
}
}
_first = false;
var radians = GetRadians(Angle);
var x = (float) (Width / 2f + radius*Math.Cos(radians));
var y = (float) (Height / 2f + radius*Math.Sin(radians));
_paint.SetStyle(Paint.Style.Fill);
canvas.DrawCircle(x, y, CircleRadius, _paint);
}
private static float GetRadians(float angle)
{
return (float) (angle*(Math.PI/180));
}
}
}
using System.Threading.Tasks;
using Android.App;
using Android.Graphics;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;
namespace CircleWaves
{
[Activity(Label = "CircleWaves", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private LinearLayout _rootLayout;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var metrics = new DisplayMetrics();
WindowManager.DefaultDisplay.GetMetrics(metrics);
var circles = 15;
var width = metrics.WidthPixels;
var padding = 6;
var viewWidth = width/circles;
var overlap = viewWidth / 2;
_rootLayout = new LinearLayout(this)
{
LayoutParameters = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent),
Orientation = Orientation.Vertical
};
_rootLayout.SetPadding(10, 10, 10, 10);
_rootLayout.SetBackgroundColor(Color.White);
// Play around with this value to get a different effect
var angleIncrement = 10;
var angle = 0;
for (var i = 0; i < circles; i++)
{
var row = new LinearLayout(this)
{
Orientation = Orientation.Horizontal
};
if (i == 0)
row.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.WrapContent);
else
row.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.WrapContent) {TopMargin = -overlap};
row.SetBackgroundColor(Color.Transparent);
_rootLayout.AddView(row);
for (var j = 0; j < circles; j++)
{
var circle = new CircleView(this)
{
CircleRadius = 8,
Angle = angle,
Increment = 12
};
circle.SetPadding(padding, padding, padding, padding);
circle.SetBackgroundColor(Color.Transparent);
row.AddView(circle, j,
j == 0
? new LinearLayout.LayoutParams(viewWidth, viewWidth)
: new LinearLayout.LayoutParams(viewWidth, viewWidth) {LeftMargin = -overlap});
angle = angle + angleIncrement;
}
angle = angleIncrement * i;
}
SetContentView(_rootLayout);
MainLoop();
}
private async Task MainLoop()
{
while (true)
{
await Task.Delay(2);
var childCount = _rootLayout.ChildCount;
for (var i = 0; i < childCount; i++)
{
var child = _rootLayout.GetChildAt(i) as LinearLayout;
if (child == null) continue;
var innerChildCount = child.ChildCount;
for (var j = 0; j < innerChildCount; j++)
{
child.GetChildAt(j).PostInvalidate();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment