Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/build-dotnet-android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build .NET Android

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'

- name: Install .NET Android workload
run: dotnet workload install android

- name: Restore dependencies
run: dotnet restore AndroidDrawing.dotnet.csproj

- name: Build library
run: dotnet build AndroidDrawing.dotnet.csproj --configuration Release --no-restore

- name: Restore example app dependencies
run: dotnet restore examples/CanvasExample/CanvasExample.csproj

- name: Build example app
run: dotnet build examples/CanvasExample/CanvasExample.csproj --configuration Release --no-restore
11 changes: 8 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
bin/*
obj/*
*.pidb
bin/
obj/
*.pidb
*.user
*.suo
.vs/
*.apk
*.aab
6 changes: 6 additions & 0 deletions examples/CanvasExample/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:supportsRtl="true" android:label="Canvas Example"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk android:minSdkVersion="23" />
</manifest>
24 changes: 24 additions & 0 deletions examples/CanvasExample/CanvasExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net9.0-android</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>CanvasExample</RootNamespace>
<SingleProject>true</SingleProject>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<ApplicationId>com.hobdrive.canvasexample</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">23.0</SupportedOSPlatformVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\AndroidDrawing.dotnet.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
</ItemGroup>

</Project>
81 changes: 81 additions & 0 deletions examples/CanvasExample/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Android.App;
using Android.OS;
using Android.Views;
using Android.Content;
using System.Drawing;

namespace CanvasExample
{
[Activity(Label = "Canvas Example", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Create a custom view to demonstrate canvas operations
var canvasView = new CanvasView(this);
SetContentView(canvasView);
}
}

public class CanvasView : View
{
public CanvasView(Context context) : base(context)
{
}

protected override void OnDraw(Android.Graphics.Canvas? canvas)
{
base.OnDraw(canvas);

if (canvas == null)
return;

// Use AndroidDrawing Graphics wrapper
var g = new Graphics(canvas);

// Set DPI for proper scaling
Graphics.DeviceDPI = (int)Resources!.DisplayMetrics!.Density * 160;

// Set line width and flags
g.LineWidth = 2;
g.Flags = Android.Graphics.PaintFlags.AntiAlias;

// Draw background
g.FillRectangle(new Brush(Color.White), 0, 0, Width, Height);

// Draw various shapes to demonstrate canvas operations

// Draw a red line
g.DrawLine(new Pen(Color.Red), 50, 50, 350, 50);

// Draw a blue rectangle
g.DrawRectangle(new Pen(Color.Blue), 50, 100, 300, 100);

// Fill a green rectangle
g.FillRectangle(new Brush(Color.Green), 70, 120, 260, 60);

// Draw an orange ellipse
g.DrawEllipse(new Pen(Color.Orange), 50, 250, 300, 150);

// Fill a purple ellipse
g.FillEllipse(new Brush(Color.Purple), 100, 280, 200, 90);

// Draw text
g.DrawString("AndroidDrawing Example",
new Font("serif", 20, FontStyle.Bold),
new Brush(Color.Black),
new RectangleF(50, 450, 300, 50));

// Draw multiline text in a rectangle
g.DrawString("This demonstrates various canvas operations:\n• Lines\n• Rectangles\n• Ellipses\n• Text rendering",
new Font("serif", 14, FontStyle.Regular),
new Brush(Color.DarkBlue),
new RectangleF(50, 520, 300, 200));

// Flush to complete drawing
g.Flush();
}
}
}
32 changes: 32 additions & 0 deletions examples/CanvasExample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Canvas Example

This is a simple Android application that demonstrates the usage of AndroidDrawing library.

## Features

The example demonstrates various canvas operations:
- Drawing lines with different colors
- Drawing and filling rectangles
- Drawing and filling ellipses
- Text rendering (single and multiline)
- Anti-aliasing support

## Building

To build the example:

```bash
dotnet build CanvasExample.csproj
```

## Running

To run on an Android device or emulator:

```bash
dotnet build -t:Run -f net9.0-android
```

## Code Overview

The example creates a simple `MainActivity` with a custom `CanvasView` that overrides the `OnDraw` method to demonstrate various drawing operations using the AndroidDrawing `Graphics` class.
4 changes: 4 additions & 0 deletions examples/CanvasExample/Resources/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Canvas Example</string>
</resources>
Loading