diff options
author | Rui Marinho <me@ruimarinho.net> | 2017-03-24 22:11:20 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-24 22:11:20 +0000 |
commit | 9dcce94937584e1ccbf52371736ee3b712f5107f (patch) | |
tree | d11544f3585205a3eccafe5fd9a0dbf4e901f5b2 | |
parent | 349969f432ab0a85844d56b26d664beffb6f8a05 (diff) | |
download | xamarin-forms-9dcce94937584e1ccbf52371736ee3b712f5107f.tar.gz xamarin-forms-9dcce94937584e1ccbf52371736ee3b712f5107f.tar.bz2 xamarin-forms-9dcce94937584e1ccbf52371736ee3b712f5107f.zip |
[Android]Refactor to use a extension method to get the drawable (#841)
* [Android]Refactor to use a extension method to get the drawable
* [Android] Use local variable on check
4 files changed, 24 insertions, 10 deletions
diff --git a/Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs b/Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs index 1b4afba2..7fe1f1e8 100644 --- a/Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs +++ b/Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs @@ -744,9 +744,9 @@ namespace Xamarin.Forms.Platform.Android.AppCompat FileImageSource icon = item.Icon; if (!string.IsNullOrEmpty(icon)) { - Drawable iconBitmap = context.Resources.GetDrawable(icon) ?? new BitmapDrawable(context.Resources, ResourceManager.GetBitmap(context.Resources, icon)); - if (iconBitmap != null) - menuItem.SetIcon(iconBitmap); + Drawable iconDrawable = context.Resources.GetFormsDrawable(icon); + if (iconDrawable != null) + menuItem.SetIcon(iconDrawable); } menuItem.SetEnabled(controller.IsEnabled); menuItem.SetShowAsAction(ShowAsAction.Always); diff --git a/Xamarin.Forms.Platform.Android/CellAdapter.cs b/Xamarin.Forms.Platform.Android/CellAdapter.cs index 187ac23c..0d894138 100644 --- a/Xamarin.Forms.Platform.Android/CellAdapter.cs +++ b/Xamarin.Forms.Platform.Android/CellAdapter.cs @@ -193,11 +193,12 @@ namespace Xamarin.Forms.Platform.Android IMenuItem item = menu.Add(Menu.None, i, Menu.None, action.Text); - if (action.Icon != null) + var icon = action.Icon; + if (icon != null) { - Drawable iconBitmap = _context.Resources.GetDrawable(action.Icon) ?? new BitmapDrawable(_context.Resources, ResourceManager.GetBitmap(_context.Resources, action.Icon)); - if (iconBitmap != null) - item.SetIcon(iconBitmap); + Drawable iconDrawable = _context.Resources.GetFormsDrawable(icon); + if (iconDrawable != null) + item.SetIcon(iconDrawable); } action.PropertyChanged += changed; diff --git a/Xamarin.Forms.Platform.Android/Platform.cs b/Xamarin.Forms.Platform.Android/Platform.cs index 309ea321..004531a1 100644 --- a/Xamarin.Forms.Platform.Android/Platform.cs +++ b/Xamarin.Forms.Platform.Android/Platform.cs @@ -362,9 +362,9 @@ namespace Xamarin.Forms.Platform.Android var icon = item.Icon; if (!string.IsNullOrEmpty(icon)) { - Drawable iconBitmap = _context.Resources.GetDrawable(icon) ?? new BitmapDrawable(_context.Resources, ResourceManager.GetBitmap(_context.Resources, icon)); - if (iconBitmap != null) - menuItem.SetIcon(iconBitmap); + Drawable iconDrawable = _context.Resources.GetFormsDrawable(icon); + if (iconDrawable != null) + menuItem.SetIcon(iconDrawable); } menuItem.SetEnabled(controller.IsEnabled); menuItem.SetShowAsAction(ShowAsAction.Always); diff --git a/Xamarin.Forms.Platform.Android/ResourceManager.cs b/Xamarin.Forms.Platform.Android/ResourceManager.cs index 0fdccaef..147fec88 100644 --- a/Xamarin.Forms.Platform.Android/ResourceManager.cs +++ b/Xamarin.Forms.Platform.Android/ResourceManager.cs @@ -17,6 +17,19 @@ namespace Xamarin.Forms.Platform.Android public static Type ResourceClass { get; set; } + internal static Drawable GetFormsDrawable(this Resources resource, FileImageSource fileImageSource) + { + var file = fileImageSource.File; + Drawable drawable = resource.GetDrawable(fileImageSource); + if(drawable == null) + { + var bitmap = GetBitmap(resource, file) ?? BitmapFactory.DecodeFile(file); + if (bitmap != null) + drawable = new BitmapDrawable(resource, bitmap); + } + return drawable; + } + public static Bitmap GetBitmap(this Resources resource, FileImageSource fileImageSource) { var file = fileImageSource.File; |