-
Notifications
You must be signed in to change notification settings - Fork 621
Expand file tree
/
Copy pathCpuFragment.cs
More file actions
96 lines (82 loc) · 2.55 KB
/
CpuFragment.cs
File metadata and controls
96 lines (82 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using Android.OS;
using Android.Views;
using AndroidX.Fragment.App;
using SkiaSharp;
using SkiaSharp.Views.Android;
namespace SkiaSharpSample;
public class CpuFragment : Fragment
{
static readonly (float X, float Y, float R, SKColor Color)[] circles =
{
(0.20f, 0.30f, 0.10f, new SKColor(0xFF, 0x4D, 0x66, 0xCC)),
(0.75f, 0.25f, 0.08f, new SKColor(0x4D, 0xB3, 0xFF, 0xCC)),
(0.15f, 0.70f, 0.07f, new SKColor(0xFF, 0x99, 0x1A, 0xCC)),
(0.80f, 0.70f, 0.12f, new SKColor(0x66, 0xFF, 0xB3, 0xCC)),
(0.50f, 0.15f, 0.06f, new SKColor(0xB3, 0x4D, 0xFF, 0xCC)),
(0.40f, 0.80f, 0.09f, new SKColor(0xFF, 0xE6, 0x33, 0xCC)),
};
static readonly SKColor[] gradientColors =
{
new SKColor(0x44, 0x88, 0xFF),
new SKColor(0x88, 0x33, 0xCC),
};
private SKCanvasView skiaView;
private SKTypeface typeface;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.fragment_cpu, container, false);
skiaView = view.FindViewById<SKCanvasView>(Resource.Id.skiaView);
skiaView.PaintSurface += OnPaintSurface;
using var stream = Context.Assets.Open("Fonts/NotoSans-Regular.ttf");
typeface = SKTypeface.FromStream(stream);
return view;
}
private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
var canvas = e.Surface.Canvas;
var width = e.Info.Width;
var height = e.Info.Height;
var center = new SKPoint(width / 2f, height / 2f);
var radius = Math.Max(width, height) / 2f;
canvas.Clear(SKColors.White);
// Background gradient
using var shader = SKShader.CreateRadialGradient(center, radius, gradientColors, SKShaderTileMode.Clamp);
using var bgPaint = new SKPaint
{
IsAntialias = true,
Shader = shader,
};
canvas.DrawRect(0, 0, width, height, bgPaint);
// Circles
using var circlePaint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Fill,
};
foreach (var (x, y, r, color) in circles)
{
circlePaint.Color = color;
canvas.DrawCircle(x * width, y * height, r * Math.Min(width, height), circlePaint);
}
// Centered text
using var textPaint = new SKPaint
{
Color = SKColors.White,
IsAntialias = true,
};
using var font = new SKFont(typeface, width * 0.12f);
canvas.DrawText("SkiaSharp", center.X, center.Y + font.Size / 3f, SKTextAlign.Center, font, textPaint);
}
public override void OnDestroyView()
{
if (skiaView != null)
{
skiaView.PaintSurface -= OnPaintSurface;
skiaView = null;
}
typeface?.Dispose();
typeface = null;
base.OnDestroyView();
}
}