[Android] Fix bug : Frame outline color not rendering (#1385)

* Fix issue G1347

* Add dispose _backgroundDrawable

* Replace OutlineColor by BorderColor
This commit is contained in:
Mohamed CHOUCHANE 2018-01-03 19:47:38 +01:00 коммит произвёл Jason Smith
Родитель b8de5564bf
Коммит c8b4685bc9
3 изменённых файлов: 71 добавлений и 3 удалений

Просмотреть файл

@ -0,0 +1,44 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Collections.ObjectModel;
#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif
namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 1347, "[Android] Frame outline color not rendering", PlatformAffected.Android)]
public class Issue1347 : TestContentPage
{
Label label;
Frame frame;
protected override void Init()
{
label = new Label() { Text = "The ouline color should be red", VerticalOptions = LayoutOptions.CenterAndExpand, HorizontalOptions = LayoutOptions.CenterAndExpand };
frame = new Frame
{
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
HeightRequest = 300,
WidthRequest = 200,
BorderColor = Color.Red,
BackgroundColor = Color.LightBlue,
HasShadow = true,
Content = label
};
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
frame.BorderColor = frame.BorderColor == Color.Default ? Color.Red : Color.Default;
frame.BackgroundColor = frame.BackgroundColor == Color.Default ? Color.LightBlue : Color.Default;
label.Text = frame.BorderColor == Color.Default ? "The ouline color should be default (click here to change color)" : "The ouline color should be red (click here to change color)";
};
frame.GestureRecognizers.Add(tapGestureRecognizer);
Content = frame;
}
}
}

Просмотреть файл

@ -264,6 +264,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue1024.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1025.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1026.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1347.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1356.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1691.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2983.cs" />

Просмотреть файл

@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using Android.Content;
using Android.Graphics.Drawables;
using Android.Support.V7.Widget;
using Android.Views;
using Xamarin.Forms.Platform.Android.FastRenderers;
@ -17,6 +18,7 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
bool _disposed;
Frame _element;
GradientDrawable _backgroundDrawable;
VisualElementPackager _visualElementPackager;
VisualElementTracker _visualElementTracker;
@ -125,7 +127,13 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
_visualElementPackager.Dispose();
_visualElementPackager = null;
}
if (_backgroundDrawable != null)
{
_backgroundDrawable.Dispose();
_backgroundDrawable = null;
}
int count = ChildCount;
for (var i = 0; i < count; i++)
{
@ -158,6 +166,9 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
if (e.NewElement != null)
{
this.EnsureId();
_backgroundDrawable = new GradientDrawable();
_backgroundDrawable.SetShape(ShapeType.Rectangle);
this.SetBackground(_backgroundDrawable);
if (_visualElementTracker == null)
{
@ -170,6 +181,7 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
UpdateShadow();
UpdateBackgroundColor();
UpdateCornerRadius();
UpdateBorderColor();
ElevationHelper.SetElevation(this, e.NewElement);
}
@ -211,6 +223,8 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
UpdateBackgroundColor();
else if (e.PropertyName == Frame.CornerRadiusProperty.PropertyName)
UpdateCornerRadius();
else if (e.PropertyName == Frame.BorderColorProperty.PropertyName)
UpdateBorderColor();
}
void UpdateBackgroundColor()
@ -219,7 +233,16 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
return;
Color bgColor = Element.BackgroundColor;
SetCardBackgroundColor(bgColor.IsDefault ? AColor.White : bgColor.ToAndroid());
_backgroundDrawable.SetColor(bgColor.IsDefault ? AColor.White : bgColor.ToAndroid());
}
void UpdateBorderColor()
{
if (_disposed)
return;
Color borderColor = Element.BorderColor;
_backgroundDrawable.SetStroke(3, borderColor.IsDefault ? AColor.White : borderColor.ToAndroid());
}
void UpdateShadow()
@ -255,7 +278,7 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
else
cornerRadius = Context.ToPixels(cornerRadius);
Radius = cornerRadius;
_backgroundDrawable.SetCornerRadius(cornerRadius);
}
}
}