Skip to content

Commit

Permalink
Fixes a few bugs
Browse files Browse the repository at this point in the history
- Fixes cornerradius regressing due to #61
- Fixes Elevation for Android < API21
  • Loading branch information
sthewissen committed Nov 11, 2019
1 parent ce07be0 commit d65b7b7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,12 @@ void DrawBackground(ACanvas canvas, int width, int height, CornerRadius cornerRa
}
else
{
path = ShapeUtils.CreateRoundedRectPath(width, height,
cornerRadius.TopLeft,
cornerRadius.TopRight,
cornerRadius.BottomRight,
cornerRadius.BottomLeft);

//using (var rect = new RectF(0, 0, width, height))
//{
// float topLeft = _convertToPixels(cornerRadius.TopLeft);
// float topRight = _convertToPixels(cornerRadius.TopRight);
// float bottomRight = _convertToPixels(cornerRadius.BottomRight);
// float bottomLeft = _convertToPixels(cornerRadius.BottomLeft);

// if (!_pancake.HasShadow || _pancake.Elevation > 0)
// path.AddRoundRect(rect, new float[] { topLeft, topLeft, topRight, topRight, bottomRight, bottomRight, bottomLeft, bottomLeft }, direction);
// else
// path.AddRoundRect(rect, new float[] { topLeft, topLeft, topLeft, topLeft, topLeft, topLeft, topLeft, topLeft }, direction);
//}
float topLeft = _convertToPixels(cornerRadius.TopLeft);
float topRight = _convertToPixels(cornerRadius.TopRight);
float bottomRight = _convertToPixels(cornerRadius.BottomRight);
float bottomLeft = _convertToPixels(cornerRadius.BottomLeft);

path = ShapeUtils.CreateRoundedRectPath(width, height, topLeft, topRight, bottomRight, bottomLeft);
}

if ((_pancake.BackgroundGradientStartColor != default(Color) && _pancake.BackgroundGradientEndColor != default(Color)) || (_pancake.BackgroundGradientStops != null && _pancake.BackgroundGradientStops.Any()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Support.V4.View;
using Android.Views;
using Xamarin.Forms;
Expand Down Expand Up @@ -76,37 +77,40 @@ private void Validate(PancakeView pancake)

private void SetupShadow(PancakeView pancake)
{
// clear previous shadow/elevation
this.Elevation = 0;
this.TranslationZ = 0;

bool hasShadowOrElevation = pancake.HasShadow || pancake.Elevation > 0;

// If it has a shadow, give it a default Droid looking shadow.
if (pancake.HasShadow)
{
this.Elevation = 10;
this.TranslationZ = 10;
}

// However if it has a specified elevation add the desired one
if (pancake.Elevation > 0)
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
// clear previous shadow/elevation
this.Elevation = 0;
this.TranslationZ = 0;
ViewCompat.SetElevation(this, Context.ToPixels(pancake.Elevation));
}

if (hasShadowOrElevation)
{
// To have shadow show up, we need to clip.
this.OutlineProvider = new RoundedCornerOutlineProvider(pancake, Context.ToPixels);
this.ClipToOutline = true;
}
else
{
this.OutlineProvider = null;
this.ClipToOutline = false;
bool hasShadowOrElevation = pancake.HasShadow || pancake.Elevation > 0;

// If it has a shadow, give it a default Droid looking shadow.
if (pancake.HasShadow)
{
this.Elevation = 10;
this.TranslationZ = 10;
}

// However if it has a specified elevation add the desired one
if (pancake.Elevation > 0)
{
this.Elevation = 0;
this.TranslationZ = 0;
ViewCompat.SetElevation(this, Context.ToPixels(pancake.Elevation));
}

if (hasShadowOrElevation)
{
// To have shadow show up, we need to clip.
this.OutlineProvider = new RoundedCornerOutlineProvider(pancake, Context.ToPixels);
this.ClipToOutline = true;
}
else
{
this.OutlineProvider = null;
this.ClipToOutline = false;
}
}
}

Expand Down Expand Up @@ -177,10 +181,10 @@ protected override void OnDraw(ACanvas canvas)
else
{
using (var path = ShapeUtils.CreateRoundedRectPath(Width, Height,
control.CornerRadius.TopLeft,
control.CornerRadius.TopRight,
control.CornerRadius.BottomRight,
control.CornerRadius.BottomLeft))
Context.ToPixels(control.CornerRadius.TopLeft),
Context.ToPixels(control.CornerRadius.TopRight),
Context.ToPixels(control.CornerRadius.BottomRight),
Context.ToPixels(control.CornerRadius.BottomLeft)))
{
canvas.Save();
canvas.ClipPath(path);
Expand Down Expand Up @@ -210,10 +214,10 @@ protected override bool DrawChild(ACanvas canvas, global::Android.Views.View chi
else
{
using (var path = ShapeUtils.CreateRoundedRectPath(Width, Height,
control.CornerRadius.TopLeft,
control.CornerRadius.TopRight,
control.CornerRadius.BottomRight,
control.CornerRadius.BottomLeft))
Context.ToPixels(control.CornerRadius.TopLeft),
Context.ToPixels(control.CornerRadius.TopRight),
Context.ToPixels(control.CornerRadius.BottomRight),
Context.ToPixels(control.CornerRadius.BottomLeft)))
{
canvas.Save();
canvas.ClipPath(path);
Expand All @@ -235,7 +239,7 @@ private void DrawBorder(ACanvas canvas, PancakeView control)
{
var borderThickness = Context.ToPixels(control.BorderThickness);
var halfBorderThickness = borderThickness / 2;
bool hasShadowOrElevation = control.HasShadow || control.Elevation > 0;
bool hasShadowOrElevation = control.HasShadow || (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop && control.Elevation > 0);

// TODO: This doesn't look entirely right yet when using it with rounded corners.
using (var paint = new Paint { AntiAlias = true })
Expand All @@ -254,10 +258,10 @@ private void DrawBorder(ACanvas canvas, PancakeView control)
else
{
path = ShapeUtils.CreateRoundedRectPath(Width, Height,
control.CornerRadius.TopLeft,
control.CornerRadius.TopRight,
control.CornerRadius.BottomRight,
control.CornerRadius.BottomLeft);
Context.ToPixels(control.CornerRadius.TopLeft),
Context.ToPixels(control.CornerRadius.TopRight),
Context.ToPixels(control.CornerRadius.BottomRight),
Context.ToPixels(control.CornerRadius.BottomLeft));
}

if (control.BorderIsDashed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ namespace Xamarin.Forms.PancakeView.Droid
{
public static class ShapeUtils
{
public static Path CreateRoundedRectPath(float rectWidth, float rectHeight, double topLeft, double topRight, double bottomRight, double bottomLeft)
public static Path CreateRoundedRectPath(float rectWidth, float rectHeight, float topLeft, float topRight, float bottomRight, float bottomLeft)
{
var path = new Path();
var radii = new[] { topLeft, topLeft,
topRight, topRight,
bottomRight, bottomRight,
bottomLeft, bottomLeft };

path.AddRoundRect(new RectF(0, 0, rectWidth, rectHeight), Array.ConvertAll(radii, x => (float)x), Path.Direction.Ccw);

path.AddRoundRect(new RectF(0, 0, rectWidth, rectHeight), radii, Path.Direction.Ccw);
path.Close();

return path;
Expand Down

0 comments on commit d65b7b7

Please # to comment.