summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/ResourceManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.Android/ResourceManager.cs')
-rw-r--r--Xamarin.Forms.Platform.Android/ResourceManager.cs71
1 files changed, 71 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/ResourceManager.cs b/Xamarin.Forms.Platform.Android/ResourceManager.cs
new file mode 100644
index 00000000..4c150a05
--- /dev/null
+++ b/Xamarin.Forms.Platform.Android/ResourceManager.cs
@@ -0,0 +1,71 @@
+using System;
+using System.Linq;
+using System.Reflection;
+using System.Threading.Tasks;
+using Android.Content.Res;
+using Android.Graphics;
+using Android.Graphics.Drawables;
+using Path = System.IO.Path;
+
+namespace Xamarin.Forms.Platform.Android
+{
+ public static class ResourceManager
+ {
+ public static Type DrawableClass { get; set; }
+
+ public static Type ResourceClass { get; set; }
+
+ public static Bitmap GetBitmap(this Resources resource, string name)
+ {
+ return BitmapFactory.DecodeResource(resource, IdFromTitle(name, DrawableClass));
+ }
+
+ public static Task<Bitmap> GetBitmapAsync(this Resources resource, string name)
+ {
+ return BitmapFactory.DecodeResourceAsync(resource, IdFromTitle(name, DrawableClass));
+ }
+
+ public static Drawable GetDrawable(this Resources resource, string name)
+ {
+ int id = IdFromTitle(name, DrawableClass);
+ if (id == 0)
+ {
+ Log.Warning("Could not load image named: {0}", name);
+ return null;
+ }
+ return resource.GetDrawable(id);
+ }
+
+ public static int GetDrawableByName(string name)
+ {
+ return IdFromTitle(name, DrawableClass);
+ }
+
+ public static int GetResourceByName(string name)
+ {
+ return IdFromTitle(name, ResourceClass);
+ }
+
+ public static void Init(Assembly masterAssembly)
+ {
+ DrawableClass = masterAssembly.GetTypes().FirstOrDefault(x => x.Name == "Drawable");
+ ResourceClass = masterAssembly.GetTypes().FirstOrDefault(x => x.Name == "Id");
+ }
+
+ internal static int IdFromTitle(string title, Type type)
+ {
+ string name = Path.GetFileNameWithoutExtension(title);
+ int id = GetId(type, name);
+ return id; // Resources.System.GetDrawable (Resource.Drawable.dashboard);
+ }
+
+ static int GetId(Type type, string propertyName)
+ {
+ FieldInfo[] props = type.GetFields();
+ FieldInfo prop = props.Select(p => p).FirstOrDefault(p => p.Name == propertyName);
+ if (prop != null)
+ return (int)prop.GetValue(type);
+ return 0;
+ }
+ }
+} \ No newline at end of file