From 84995a921221811d2e7047d54dcefde0c8063397 Mon Sep 17 00:00:00 2001 From: adrianknight89 Date: Thu, 3 Nov 2016 13:38:38 -0500 Subject: [Android] Allow path-based icons to be loaded as toolbar icons (#437) * Created a bitmap method to check for resource as well as path * Update platform and navigationrenderer to get images from two locations * CellAdapter could use path-based icon finding * sample app to test toolbaritem icons --- .../AppCompat/NavigationPageRenderer.cs | 4 ++-- Xamarin.Forms.Platform.Android/CellAdapter.cs | 7 ++++++- Xamarin.Forms.Platform.Android/Platform.cs | 9 ++++++--- Xamarin.Forms.Platform.Android/ResourceManager.cs | 11 +++++++++++ 4 files changed, 25 insertions(+), 6 deletions(-) (limited to 'Xamarin.Forms.Platform.Android') diff --git a/Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs b/Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs index b77063ee..dc3c5666 100644 --- a/Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs +++ b/Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs @@ -709,8 +709,8 @@ namespace Xamarin.Forms.Platform.Android.AppCompat FileImageSource icon = item.Icon; if (!string.IsNullOrEmpty(icon)) { - Drawable iconBitmap = context.Resources.GetDrawable(icon); - if (iconBitmap != null) + var iconBitmap = new BitmapDrawable(context.Resources, ResourceManager.GetBitmap(context.Resources, icon)); + if (iconBitmap != null && iconBitmap.Bitmap != null) menuItem.SetIcon(iconBitmap); } menuItem.SetEnabled(controller.IsEnabled); diff --git a/Xamarin.Forms.Platform.Android/CellAdapter.cs b/Xamarin.Forms.Platform.Android/CellAdapter.cs index 44bcb3d9..02bf7a38 100644 --- a/Xamarin.Forms.Platform.Android/CellAdapter.cs +++ b/Xamarin.Forms.Platform.Android/CellAdapter.cs @@ -8,6 +8,7 @@ using Android.Views; using Android.Widget; using AView = Android.Views.View; using AListView = Android.Widget.ListView; +using Android.Graphics.Drawables; namespace Xamarin.Forms.Platform.Android { @@ -194,7 +195,11 @@ namespace Xamarin.Forms.Platform.Android IMenuItem item = menu.Add(Menu.None, i, Menu.None, action.Text); if (action.Icon != null) - item.SetIcon(_context.Resources.GetDrawable(action.Icon)); + { + var iconBitmap = new BitmapDrawable(_context.Resources, ResourceManager.GetBitmap(_context.Resources, action.Icon)); + if (iconBitmap != null && iconBitmap.Bitmap != null) + item.SetIcon(_context.Resources.GetDrawable(action.Icon)); + } action.PropertyChanged += changed; action.PropertyChanging += changing; diff --git a/Xamarin.Forms.Platform.Android/Platform.cs b/Xamarin.Forms.Platform.Android/Platform.cs index 3334ba6e..29af59f7 100644 --- a/Xamarin.Forms.Platform.Android/Platform.cs +++ b/Xamarin.Forms.Platform.Android/Platform.cs @@ -361,8 +361,8 @@ namespace Xamarin.Forms.Platform.Android IMenuItem menuItem = menu.Add(item.Text); if (!string.IsNullOrEmpty(item.Icon)) { - Drawable iconBitmap = _context.Resources.GetDrawable(item.Icon); - if (iconBitmap != null) + var iconBitmap = new BitmapDrawable(_context.Resources, ResourceManager.GetBitmap(_context.Resources, item.Icon)); + if (iconBitmap != null && iconBitmap.Bitmap != null) menuItem.SetIcon(iconBitmap); } menuItem.SetEnabled(controller.IsEnabled); @@ -948,7 +948,10 @@ namespace Xamarin.Forms.Platform.Android FileImageSource titleIcon = NavigationPage.GetTitleIcon(view); if (!string.IsNullOrWhiteSpace(titleIcon)) { - actionBar.SetLogo(_context.Resources.GetDrawable(titleIcon)); + var iconBitmap = new BitmapDrawable(_context.Resources, ResourceManager.GetBitmap(_context.Resources, titleIcon)); + if (iconBitmap != null && iconBitmap.Bitmap != null) + actionBar.SetLogo(iconBitmap); + useLogo = true; showHome = true; showTitle = true; diff --git a/Xamarin.Forms.Platform.Android/ResourceManager.cs b/Xamarin.Forms.Platform.Android/ResourceManager.cs index b8629026..397b3c27 100644 --- a/Xamarin.Forms.Platform.Android/ResourceManager.cs +++ b/Xamarin.Forms.Platform.Android/ResourceManager.cs @@ -16,6 +16,17 @@ namespace Xamarin.Forms.Platform.Android public static Type ResourceClass { get; set; } + public static Bitmap GetBitmap(this Resources resource, FileImageSource fileImageSource) + { + var file = fileImageSource.File; + + var bitmap = GetBitmap(resource, file); + if (bitmap != null) + return bitmap; + + return BitmapFactory.DecodeFile(file); + } + public static Bitmap GetBitmap(this Resources resource, string name) { return BitmapFactory.DecodeResource(resource, IdFromTitle(name, DrawableClass)); -- cgit v1.2.3