blob: 9a22c5707d22972602e7966012f6527993123437 (
plain)
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
|
using System;
using System.Runtime.CompilerServices;
using Android.Content;
using Android.Util;
using Android.Views.InputMethods;
namespace Xamarin.Forms.Platform.Android
{
public static class ContextExtensions
{
static float s_displayDensity = float.MinValue;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double FromPixels(this Context self, double pixels)
{
SetupMetrics(self);
return pixels / s_displayDensity;
}
public static void HideKeyboard(this Context self, global::Android.Views.View view)
{
var service = (InputMethodManager)self.GetSystemService(Context.InputMethodService);
service.HideSoftInputFromWindow(view.WindowToken, 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float ToPixels(this Context self, double dp)
{
SetupMetrics(self);
return (float)Math.Round(dp * s_displayDensity);
}
internal static double GetThemeAttributeDp(this Context self, int resource)
{
using(var value = new TypedValue())
{
if (!self.Theme.ResolveAttribute(resource, value, true))
return -1;
var pixels = (double)TypedValue.ComplexToDimension(value.Data, self.Resources.DisplayMetrics);
return self.FromPixels(pixels);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void SetupMetrics(Context context)
{
if (s_displayDensity != float.MinValue)
return;
using(DisplayMetrics metrics = context.Resources.DisplayMetrics)
s_displayDensity = metrics.Density;
}
}
}
|