Created
October 6, 2019 07:41
-
-
Save ufukhawk/169880ee14a7a253bdc2ae30c34f5fbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: ExportRendererAttribute(typeof(IconView), typeof(IconViewRenderer))] | |
namespace IconApp.Droid.Renderers | |
{ | |
public class IconViewRenderer : ViewRenderer<IconView, ImageView> | |
{ | |
private bool _isDisposed; | |
public IconViewRenderer() | |
{ | |
base.AutoPackage = false; | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (_isDisposed) | |
{ | |
return; | |
} | |
_isDisposed = true; | |
base.Dispose(disposing); | |
} | |
protected override void OnElementChanged(ElementChangedEventArgs<IconView> e) | |
{ | |
base.OnElementChanged(e); | |
if (e.OldElement == null) | |
{ | |
SetNativeControl(new ImageView(Context)); | |
} | |
UpdateBitmap(e.OldElement); | |
} | |
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) | |
{ | |
base.OnElementPropertyChanged(sender, e); | |
if (e.PropertyName == IconView.SourceProperty.PropertyName) | |
{ | |
UpdateBitmap(null); | |
} | |
else if (e.PropertyName == IconView.ForegroundProperty.PropertyName) | |
{ | |
UpdateBitmap(null); | |
} | |
} | |
private void UpdateBitmap(IconView previous = null) | |
{ | |
if (!_isDisposed && !string.IsNullOrWhiteSpace(Element.Source)) | |
{ | |
var d = Resources.GetDrawable(Element.Source).Mutate(); | |
d.SetColorFilter(new LightingColorFilter(Element.Foreground.ToAndroid(), Element.Foreground.ToAndroid())); | |
d.Alpha = Element.Foreground.ToAndroid().A; | |
Control.SetImageDrawable(d); | |
((IVisualElementController)Element).NativeSizeChanged(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment