-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from muak/development
Add Gradient
- Loading branch information
Showing
13 changed files
with
783 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using AiForms.Effects; | ||
using AiForms.Effects.Droid; | ||
using Android.Graphics.Drawables; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Platform.Android; | ||
|
||
[assembly: ExportEffect(typeof(GradientPlatformEffect), nameof(Gradient))] | ||
namespace AiForms.Effects.Droid | ||
{ | ||
public class GradientPlatformEffect:AiEffectBase | ||
{ | ||
Android.Views.View _view; | ||
GradientDrawable _gradient; | ||
Drawable _orgDrawable; | ||
|
||
protected override void OnAttachedOverride() | ||
{ | ||
_view = Container ?? Control; | ||
|
||
_gradient = new GradientDrawable(); | ||
_orgDrawable = _view.Background; | ||
|
||
UpdateGradient(); | ||
} | ||
|
||
protected override void OnDetachedOverride() | ||
{ | ||
if(!IsDisposed) | ||
{ | ||
_view.Background = _orgDrawable; | ||
_view.ClipToOutline = false; | ||
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing"); | ||
} | ||
|
||
_gradient?.Dispose(); | ||
_gradient = null; | ||
_view = null; | ||
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely"); | ||
} | ||
|
||
protected override void OnElementPropertyChanged(PropertyChangedEventArgs args) | ||
{ | ||
base.OnElementPropertyChanged(args); | ||
|
||
if (!IsSupportedByApi) | ||
return; | ||
|
||
if (IsDisposed) | ||
{ | ||
return; | ||
} | ||
|
||
if (args.PropertyName == Gradient.ColorsProperty.PropertyName || | ||
args.PropertyName == Gradient.OrientationProperty.PropertyName) | ||
{ | ||
UpdateGradient(); | ||
} | ||
} | ||
|
||
void UpdateGradient() | ||
{ | ||
var colors = Gradient.GetColors(Element); | ||
if(colors == null) | ||
{ | ||
return; | ||
} | ||
|
||
_gradient.SetColors(colors.Select(x => (int)x.ToAndroid()).ToArray()); | ||
_gradient.SetOrientation(ConvertOrientation()); | ||
|
||
_view.ClipToOutline = true; //not to overflow children | ||
_view.SetBackground(_gradient); | ||
} | ||
|
||
GradientDrawable.Orientation ConvertOrientation() | ||
{ | ||
var orientation = Gradient.GetOrientation(Element); | ||
|
||
switch (orientation) | ||
{ | ||
case GradientOrientation.LeftRight: | ||
return GradientDrawable.Orientation.LeftRight; | ||
case GradientOrientation.BlTr: | ||
return GradientDrawable.Orientation.BlTr; | ||
case GradientOrientation.BottomTop: | ||
return GradientDrawable.Orientation.BottomTop; | ||
case GradientOrientation.BrTl: | ||
return GradientDrawable.Orientation.BrTl; | ||
case GradientOrientation.RightLeft: | ||
return GradientDrawable.Orientation.RightLeft; | ||
case GradientOrientation.TrBl: | ||
return GradientDrawable.Orientation.TrBl; | ||
case GradientOrientation.TopBottom: | ||
return GradientDrawable.Orientation.TopBottom; | ||
case GradientOrientation.TlBr: | ||
return GradientDrawable.Orientation.TlBr; | ||
default: | ||
return GradientDrawable.Orientation.LeftRight; | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using AiForms.Effects; | ||
using AiForms.Effects.iOS; | ||
using CoreAnimation; | ||
using CoreGraphics; | ||
using UIKit; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Platform.iOS; | ||
|
||
[assembly: ExportEffect(typeof(GradientPlatformEffect), nameof(Gradient))] | ||
namespace AiForms.Effects.iOS | ||
{ | ||
public class GradientPlatformEffect:PlatformEffect | ||
{ | ||
UIView _view; | ||
VisualElement _visualElement; | ||
CAGradientLayer _layer; | ||
bool _clipsToBounds; | ||
|
||
protected override void OnAttached() | ||
{ | ||
_view = Control ?? Container; | ||
_visualElement = Element as VisualElement; | ||
if(_visualElement == null) | ||
{ | ||
Device.BeginInvokeOnMainThread(() => Gradient.SetOn(Element, false)); | ||
return; | ||
} | ||
|
||
if (Element is Label) | ||
{ | ||
_view = Container; | ||
} | ||
|
||
_clipsToBounds = _view.ClipsToBounds; | ||
_visualElement.SizeChanged += OnSizeChanged; | ||
UpdateGradient(); | ||
} | ||
|
||
protected override void OnDetached() | ||
{ | ||
if(_visualElement != null) | ||
{ | ||
_visualElement.SizeChanged -= OnSizeChanged; | ||
} | ||
|
||
if(_view != null) | ||
{ | ||
_view.ClipsToBounds = _clipsToBounds; | ||
} | ||
|
||
_layer?.RemoveFromSuperLayer(); | ||
_layer?.Dispose(); | ||
_layer = null; | ||
|
||
_view = null; | ||
_visualElement = null; | ||
|
||
System.Diagnostics.Debug.WriteLine($"Detached {GetType().Name} from {Element.GetType().FullName}"); | ||
} | ||
|
||
protected override void OnElementPropertyChanged(PropertyChangedEventArgs args) | ||
{ | ||
base.OnElementPropertyChanged(args); | ||
if(args.PropertyName == Gradient.ColorsProperty.PropertyName || | ||
args.PropertyName == Gradient.OrientationProperty.PropertyName) | ||
{ | ||
UpdateGradient(); | ||
} | ||
} | ||
|
||
private void OnSizeChanged(object sender, EventArgs e) | ||
{ | ||
UpdateGradient(); | ||
} | ||
|
||
void UpdateGradient() | ||
{ | ||
var colors = Gradient.GetColors(Element); | ||
if(colors == null) | ||
{ | ||
return; | ||
} | ||
|
||
_layer?.RemoveFromSuperLayer(); | ||
_layer?.Dispose(); | ||
|
||
_layer = new CAGradientLayer(); | ||
_layer.Frame = new CGRect(0, 0, _visualElement.Width, _visualElement.Height); | ||
_layer.Colors = colors.Select(x => x.ToCGColor()).ToArray(); | ||
(_layer.StartPoint,_layer.EndPoint) = ConvertPoint(); | ||
_view.Layer.InsertSublayer(_layer, 0); | ||
_view.ClipsToBounds = true; | ||
} | ||
|
||
(CGPoint start,CGPoint end) ConvertPoint() | ||
{ | ||
var orientation = Gradient.GetOrientation(Element); | ||
|
||
switch(orientation) | ||
{ | ||
case GradientOrientation.LeftRight: | ||
return (new CGPoint(0, 0.5), new CGPoint(1, 0.5)); | ||
case GradientOrientation.BlTr: | ||
return (new CGPoint(0, 1.0), new CGPoint(1, 0)); | ||
case GradientOrientation.BottomTop: | ||
return (new CGPoint(0.5, 1), new CGPoint(0.5, 0)); | ||
case GradientOrientation.BrTl: | ||
return (new CGPoint(1, 1), new CGPoint(0, 0)); | ||
case GradientOrientation.RightLeft: | ||
return (new CGPoint(1, 0.5), new CGPoint(0, 0.5)); | ||
case GradientOrientation.TrBl: | ||
return (new CGPoint(1, 0), new CGPoint(0, 1)); | ||
case GradientOrientation.TopBottom: | ||
return (new CGPoint(0.5, 0), new CGPoint(0.5, 1)); | ||
case GradientOrientation.TlBr: | ||
return (new CGPoint(0, 0), new CGPoint(1, 1)); | ||
default: | ||
return (new CGPoint(0, 0.5), new CGPoint(1, 0.5)); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.