forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextExtensions.Android.cs
More file actions
45 lines (39 loc) · 880 Bytes
/
Copy pathContextExtensions.Android.cs
File metadata and controls
45 lines (39 loc) · 880 Bytes
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Runtime.CompilerServices;
using Android.Util;
namespace Android.Content
{
public static class ContextExtensions
{
private static float? _density;
public static float ToPixels(this Android.Content.Context self, float dp)
{
Init(self);
return (float)Math.Round(dp * _density.Value);
}
public static float FromPixels(this Android.Content.Context self, float pixels)
{
Init(self);
return pixels / _density.Value;
}
private static void Init(Android.Content.Context context)
{
if (_density != null)
{
return;
}
using (var displayMetrics = context.Resources.DisplayMetrics)
{
_density = displayMetrics.Density;
}
}
}
}