Skip to content

Commit 9fda72c

Browse files
author
cpetzold
committed
Adding AnalogClock sample
AnalogClock sample for iOS, Android, WinPhone
1 parent af3c013 commit 9fda72c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2197
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ MWC/MWC.Core/Bin/*
2020
MWC/MWC.Droid/Bin/*
2121
MWC/MWC.Droid/Resources/Resource.Designer.cs
2222
MWC/MWC.WP7/Bin/*
23-
MWC/MWC.Core.WP7/Bin/Debug/*
23+
MWC/MWC.Core.WP7/Bin/Debug/*
24+
*.db
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>12.0.0</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{992B695F-39B4-4D20-A4B4-DA619B9893C6}</ProjectGuid>
9+
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
10+
<OutputType>Library</OutputType>
11+
<RootNamespace>AnalogClock.Android</RootNamespace>
12+
<AndroidApplication>True</AndroidApplication>
13+
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
14+
<AndroidResgenClass>Resource</AndroidResgenClass>
15+
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
16+
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
17+
<AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk>
18+
<AssemblyName>AnalogClock.Android</AssemblyName>
19+
<TargetFrameworkVersion>
20+
</TargetFrameworkVersion>
21+
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
22+
<AndroidSupportedAbis>armeabi,armeabi-v7a,x86</AndroidSupportedAbis>
23+
<AndroidStoreUncompressedFileExtensions />
24+
<MandroidI18n />
25+
<JavaMaximumHeapSize />
26+
<JavaOptions />
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
29+
<DebugSymbols>true</DebugSymbols>
30+
<DebugType>full</DebugType>
31+
<Optimize>false</Optimize>
32+
<OutputPath>bin\Debug</OutputPath>
33+
<DefineConstants>DEBUG;__MOBILE__;__ANDROID__;</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
<AndroidLinkMode>None</AndroidLinkMode>
37+
<ConsolePause>false</ConsolePause>
38+
</PropertyGroup>
39+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
40+
<DebugType>full</DebugType>
41+
<Optimize>true</Optimize>
42+
<OutputPath>bin\Release</OutputPath>
43+
<DefineConstants>__MOBILE__;__ANDROID__;</DefineConstants>
44+
<ErrorReport>prompt</ErrorReport>
45+
<WarningLevel>4</WarningLevel>
46+
<ConsolePause>false</ConsolePause>
47+
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
48+
</PropertyGroup>
49+
<ItemGroup>
50+
<Reference Include="System" />
51+
<Reference Include="System.Xml" />
52+
<Reference Include="System.Core" />
53+
<Reference Include="Mono.Android" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<Compile Include="Resources\Resource.designer.cs" />
57+
<Compile Include="Properties\AssemblyInfo.cs" />
58+
<Compile Include="ClockView.cs" />
59+
<Compile Include="MainActivity.cs" />
60+
<Compile Include="..\AnalogClock.Common\ClockModel.cs">
61+
<Link>ClockModel.cs</Link>
62+
</Compile>
63+
</ItemGroup>
64+
<ItemGroup>
65+
<None Include="Resources\AboutResources.txt" />
66+
<None Include="Assets\AboutAssets.txt" />
67+
<None Include="Properties\AndroidManifest.xml" />
68+
</ItemGroup>
69+
<ItemGroup>
70+
<AndroidResource Include="Resources\values\Strings.xml" />
71+
<AndroidResource Include="Resources\drawable\Icon.png" />
72+
</ItemGroup>
73+
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
74+
<ItemGroup>
75+
<Folder Include="Resources\layout\" />
76+
</ItemGroup>
77+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Any raw assets you want to be deployed with your application can be placed in
2+
this directory (and child directories) and given a Build Action of "AndroidAsset".
3+
4+
These files will be deployed with your package and will be accessible using Android's
5+
AssetManager, like this:
6+
7+
public class ReadAsset : Activity
8+
{
9+
protected override void OnCreate (Bundle bundle)
10+
{
11+
base.OnCreate (bundle);
12+
13+
InputStream input = Assets.Open ("my_asset.txt");
14+
}
15+
}
16+
17+
Additionally, some Android functions will automatically load asset files:
18+
19+
Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
using System;
2+
using Android.App;
3+
using Android.Content;
4+
using Android.Graphics;
5+
using Android.OS;
6+
using Android.Runtime;
7+
using Android.Util;
8+
using Android.Views;
9+
using Android.Widget;
10+
11+
namespace AnalogClock.Android
12+
{
13+
public class ClockView : View
14+
{
15+
Path tickMarks;
16+
Path hourHand;
17+
Path minuteHand;
18+
Path secondHand;
19+
20+
Paint paint = new Paint();
21+
DashPathEffect minuteTickDashEffect =
22+
new DashPathEffect (new float[] { 0.1f, 3 * (float)Math.PI - 0.1f}, 0);
23+
24+
DashPathEffect hourTickDashEffect =
25+
new DashPathEffect (new float[] { 0.1f, 15 * (float)Math.PI - 0.1f}, 0);
26+
27+
float hourAngle, minuteAngle, secondAngle;
28+
29+
public ClockView (Context context) :
30+
base (context)
31+
{
32+
Initialize ();
33+
}
34+
35+
public ClockView (Context context, IAttributeSet attrs) :
36+
base (context, attrs)
37+
{
38+
Initialize ();
39+
}
40+
41+
public ClockView (Context context, IAttributeSet attrs, int defStyle) :
42+
base (context, attrs, defStyle)
43+
{
44+
Initialize ();
45+
}
46+
47+
void Initialize ()
48+
{
49+
// Create Paint for all drawing
50+
paint = new Paint ();
51+
52+
// All paths are based on 100-unit clock radius
53+
// centered at (0, 0).
54+
55+
// Define circle for tick marks.
56+
tickMarks = new Path ();
57+
tickMarks.AddCircle (0, 0, 90, Path.Direction.Cw);
58+
59+
// Hour, minute, second hands defined to point straight up.
60+
61+
// Define hour hand.
62+
hourHand = new Path ();
63+
hourHand.MoveTo (0, -60);
64+
hourHand.CubicTo (0, -30, 20, -30, 5, -20);
65+
hourHand.LineTo (5, 0);
66+
hourHand.CubicTo (5, 7.5f, -5, 7.5f, -5, 0);
67+
hourHand.LineTo (-5, -20);
68+
hourHand.CubicTo (-20, -30, 0, -30, 0, -60);
69+
hourHand.Close ();
70+
71+
// Define minute hand.
72+
minuteHand = new Path ();
73+
minuteHand.MoveTo (0, -80);
74+
minuteHand.CubicTo (0, -75, 0, -70, 2.5f, -60);
75+
minuteHand.LineTo (2.5f, 0);
76+
minuteHand.CubicTo (2.5f, 5, -2.5f, 5, -2.5f, 0);
77+
minuteHand.LineTo (-2.5f, -60);
78+
minuteHand.CubicTo (0, -70, 0, -75, 0, -80);
79+
minuteHand.Close ();
80+
81+
// Define second hand.
82+
secondHand = new Path ();
83+
secondHand.MoveTo (0, 10);
84+
secondHand.LineTo (0, -80);
85+
}
86+
87+
// Called from MainActivity.
88+
public void SetClockHandAngles(float hourAngle, float minuteAngle, float secondAngle)
89+
{
90+
this.hourAngle = hourAngle;
91+
this.minuteAngle = minuteAngle;
92+
this.secondAngle = secondAngle;
93+
94+
this.Invalidate ();
95+
}
96+
97+
protected override void OnDraw (Canvas canvas)
98+
{
99+
base.OnDraw (canvas);
100+
101+
// Clear screen to pink.
102+
paint.Color = new Color (255, 204, 204);
103+
canvas.DrawPaint (paint);
104+
105+
// Overall transforms to shift (0, 0) to center and scale.
106+
canvas.Translate (this.Width / 2, this.Height / 2);
107+
float scale = Math.Min (this.Width, this.Height) / 2.0f / 100;
108+
canvas.Scale (scale, scale);
109+
110+
// Attributes for tick marks.
111+
paint.Color = Color.Black;
112+
paint.StrokeCap = Paint.Cap.Round;
113+
paint.SetStyle (Paint.Style.Stroke);
114+
115+
// Set line dash to draw tick marks for every minute.
116+
paint.StrokeWidth = 3;
117+
paint.SetPathEffect (minuteTickDashEffect);
118+
canvas.DrawPath (tickMarks, paint);
119+
120+
// Set line dash to draw tick marks for every hour.
121+
paint.StrokeWidth = 6;
122+
paint.SetPathEffect (hourTickDashEffect);
123+
canvas.DrawPath (tickMarks, paint);
124+
125+
// Set attributes common to all clock hands.
126+
Color strokeColor = Color.Black;
127+
Color fillColor = Color.Blue;
128+
paint.StrokeWidth = 2;
129+
paint.SetPathEffect (null);
130+
131+
// Draw hour hand.
132+
canvas.Save ();
133+
canvas.Rotate (this.hourAngle);
134+
paint.Color = fillColor;
135+
paint.SetStyle (Paint.Style.Fill);
136+
canvas.DrawPath (hourHand, paint);
137+
paint.Color = strokeColor;
138+
paint.SetStyle (Paint.Style.Stroke);
139+
canvas.DrawPath (hourHand, paint);
140+
canvas.Restore ();
141+
142+
// Draw minute hand.
143+
canvas.Save ();
144+
canvas.Rotate (this.minuteAngle);
145+
paint.Color = fillColor;
146+
paint.SetStyle (Paint.Style.Fill);
147+
canvas.DrawPath (minuteHand, paint);
148+
paint.Color = strokeColor;
149+
paint.SetStyle (Paint.Style.Stroke);
150+
canvas.DrawPath (minuteHand, paint);
151+
canvas.Restore ();
152+
153+
// Draw second hand.
154+
canvas.Save ();
155+
canvas.Rotate (this.secondAngle);
156+
paint.Color = strokeColor;
157+
paint.SetStyle (Paint.Style.Stroke);
158+
canvas.DrawPath (secondHand, paint);
159+
canvas.Restore ();
160+
}
161+
}
162+
}
163+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.ComponentModel;
3+
using Android.App;
4+
using Android.Content;
5+
using Android.Runtime;
6+
using Android.Views;
7+
using Android.Widget;
8+
using Android.OS;
9+
10+
// for ClockModel.
11+
using AnalogClock.Common;
12+
13+
namespace AnalogClock.Android
14+
{
15+
[Activity (Label = "AndroidAnalogClock", MainLauncher = true)]
16+
public class MainActivity : Activity
17+
{
18+
ClockView clockView;
19+
ClockModel clockModel;
20+
21+
protected override void OnCreate (Bundle bundle)
22+
{
23+
base.OnCreate (bundle);
24+
25+
// Set content view to FrameLayout with ClockView.
26+
FrameLayout frameLayout = new FrameLayout (this);
27+
clockView = new ClockView (this);
28+
frameLayout.AddView (clockView);
29+
SetContentView (frameLayout);
30+
31+
// Create ClockModel to keep clock updated.
32+
clockModel = new ClockModel {
33+
IsSweepSecondHand = true
34+
};
35+
36+
// Initialize clock.
37+
SetClockHandAngles ();
38+
39+
clockModel.PropertyChanged += (object sender, PropertyChangedEventArgs e) =>
40+
{
41+
// Update clock.
42+
SetClockHandAngles();
43+
};
44+
}
45+
46+
void SetClockHandAngles()
47+
{
48+
clockView.SetClockHandAngles(clockModel.HourAngle,
49+
clockModel.MinuteAngle,
50+
clockModel.SecondAngle);
51+
}
52+
}
53+
}
54+
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="AnalogClock.Android">
3+
<uses-sdk android:minSdkVersion="15" />
4+
<application android:label="AnalogClock.Android"></application>
5+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using Android.App;
4+
5+
// Information about this assembly is defined by the following attributes.
6+
// Change them to the values specific to your project.
7+
[assembly: AssemblyTitle ("AnalogClock.Android")]
8+
[assembly: AssemblyDescription ("")]
9+
[assembly: AssemblyConfiguration ("")]
10+
[assembly: AssemblyCompany ("")]
11+
[assembly: AssemblyProduct ("")]
12+
[assembly: AssemblyCopyright ("charlespetzold")]
13+
[assembly: AssemblyTrademark ("")]
14+
[assembly: AssemblyCulture ("")]
15+
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
16+
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
17+
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
18+
[assembly: AssemblyVersion ("1.0.0")]
19+
// The following attributes are used to specify the signing key for the assembly,
20+
// if desired. See the Mono documentation for more information about signing.
21+
//[assembly: AssemblyDelaySign(false)]
22+
//[assembly: AssemblyKeyFile("")]
23+

0 commit comments

Comments
 (0)