Add BoxViewClock sample

This commit is contained in:
Frank A. Krueger 2017-11-10 09:57:45 -08:00
parent 5cdd3b9f48
commit 5575578c2d
6 changed files with 143 additions and 3 deletions

View File

@ -110,7 +110,13 @@ namespace Xamarin.Forms
public void StartTimer (TimeSpan interval, Func<bool> callback)
{
throw new NotImplementedException ();
Timer timer = null;
timer = new Timer ((_ => {
if (!callback ()) {
timer?.Dispose ();
timer = null;
}
}), null, (int)interval.TotalMilliseconds, (int)interval.TotalMilliseconds);
}
}

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Xamarin.Forms" Version="2.4.0.38779" />
</ItemGroup>
<ItemGroup>
@ -19,5 +20,6 @@
<ProjectReference Include="..\..\Ooui.AspNetCore\Ooui.AspNetCore.csproj" />
<ProjectReference Include="..\..\Ooui\Ooui.csproj" />
<ProjectReference Include="..\..\Samples\Samples.csproj" />
<ProjectReference Include="..\..\Ooui.Forms\Ooui.Forms.csproj" />
</ItemGroup>
</Project>

View File

@ -39,6 +39,8 @@ namespace AspNetCoreMvc
app.UseOoui ();
Xamarin.Forms.Forms.Init ();
app.UseMvc (routes => {
routes.MapRoute (
name: "default",

View File

@ -0,0 +1,130 @@
using System;
using Xamarin.Forms;
namespace Samples
{
public class BoxViewClockSample : ISample
{
public string Title => "Xamarin.Forms BoxViewClock";
public Ooui.Element CreateElement ()
{
return new BoxViewClockPage ().CreateElement ();
}
class BoxViewClockPage : ContentPage
{
// Structure for storing information about the three hands.
struct HandParams
{
public HandParams (double width, double height, double offset) : this ()
{
Width = width;
Height = height;
Offset = offset;
}
public double Width { private set; get; } // fraction of radius
public double Height { private set; get; } // ditto
public double Offset { private set; get; } // relative to center pivot
}
static readonly HandParams secondParams = new HandParams (0.02, 1.1, 0.85);
static readonly HandParams minuteParams = new HandParams (0.05, 0.8, 0.9);
static readonly HandParams hourParams = new HandParams (0.125, 0.65, 0.9);
BoxView[] tickMarks = new BoxView[60];
BoxView secondHand, minuteHand, hourHand;
public BoxViewClockPage ()
{
AbsoluteLayout absoluteLayout = new AbsoluteLayout ();
// Create the tick marks (to be sized and positioned later)
for (int i = 0; i < tickMarks.Length; i++) {
tickMarks[i] = new BoxView {
Color = Color.Accent
};
absoluteLayout.Children.Add (tickMarks[i]);
}
// Create the three hands.
absoluteLayout.Children.Add (hourHand =
new BoxView {
Color = Color.Accent
});
absoluteLayout.Children.Add (minuteHand =
new BoxView {
Color = Color.Accent
});
absoluteLayout.Children.Add (secondHand =
new BoxView {
Color = Color.Accent
});
Content = absoluteLayout;
// Attach a couple event handlers.
Device.StartTimer (TimeSpan.FromMilliseconds (16), OnTimerTick);
SizeChanged += OnPageSizeChanged;
}
void OnPageSizeChanged (object sender, EventArgs args)
{
// Size and position the 12 tick marks.
Point center = new Point (this.Width / 2, this.Height / 2);
double radius = 0.45 * Math.Min (this.Width, this.Height);
for (int i = 0; i < tickMarks.Length; i++) {
double size = radius / (i % 5 == 0 ? 15 : 30);
double radians = i * 2 * Math.PI / tickMarks.Length;
double x = center.X + radius * Math.Sin (radians) - size / 2;
double y = center.Y - radius * Math.Cos (radians) - size / 2;
AbsoluteLayout.SetLayoutBounds (tickMarks[i], new Rectangle (x, y, size, size));
tickMarks[i].AnchorX = 0.51; // Anchor settings necessary for Android
tickMarks[i].AnchorY = 0.51;
tickMarks[i].Rotation = 180 * radians / Math.PI;
}
// Function for positioning and sizing hands.
Action<BoxView, HandParams> Layout = (boxView, handParams) => {
double width = handParams.Width * radius;
double height = handParams.Height * radius;
double offset = handParams.Offset;
AbsoluteLayout.SetLayoutBounds (boxView,
new Rectangle (center.X - 0.5 * width,
center.Y - offset * height,
width, height));
boxView.AnchorX = 0.51;
boxView.AnchorY = handParams.Offset;
};
Layout (secondHand, secondParams);
Layout (minuteHand, minuteParams);
Layout (hourHand, hourParams);
}
bool OnTimerTick ()
{
// Set rotation angles for hour and minute hands.
DateTime dateTime = DateTime.Now;
hourHand.Rotation = 30 * (dateTime.Hour % 12) + 0.5 * dateTime.Minute;
minuteHand.Rotation = 6 * dateTime.Minute + 0.1 * dateTime.Second;
// Do an animation for the second hand.
double t = dateTime.Millisecond / 1000.0;
if (t < 0.5) {
t = 0.5 * Easing.SpringIn.Ease (t / 0.5);
}
else {
t = 0.5 * (1 + Easing.SpringOut.Ease ((t - 0.5) / 0.5));
}
secondHand.Rotation = 6 * (dateTime.Second + t);
return true;
}
}
}
}

View File

@ -7,6 +7,8 @@ namespace Samples
{
static void Main (string[] args)
{
Xamarin.Forms.Forms.Init ();
for (var i = 0; i < args.Length; i++) {
var a = args[i];
switch (args[i]) {

View File

@ -10,8 +10,6 @@ namespace Samples
Page MakePage ()
{
Forms.Init ();
var countLabel = new Label {
Text = "0",
BackgroundColor = Color.Gold,