Created
February 1, 2016 21:31
-
-
Save QiMata/cb96860873b4eafb6d5b to your computer and use it in GitHub Desktop.
Only works for Android 4.2 or higher. The android renderer for the ContentViewRoundedCorners control.
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
class ContentViewRoundedCornersRenderer : VisualElementRenderer<ContentView> | |
{ | |
private float _cornerRadius; | |
private RectF _bounds; | |
private Path _path; | |
protected override void OnElementChanged(ElementChangedEventArgs<ContentView> e) | |
{ | |
base.OnElementChanged(e); | |
if (e.OldElement != null) | |
{ | |
return; | |
} | |
var element = (ContentViewRoundedCorners) Element; | |
_cornerRadius = TypedValue.ApplyDimension(ComplexUnitType.Dip, element.CornerRadius, | |
Context.Resources.DisplayMetrics); | |
} | |
protected override void OnSizeChanged(int w, int h, int oldw, int oldh) | |
{ | |
base.OnSizeChanged(w, h, oldw, oldh); | |
if (w != oldw && h != oldh) | |
{ | |
_bounds = new RectF(0, 0, w, h); | |
} | |
_path = new Path(); | |
_path.Reset(); | |
_path.AddRoundRect(_bounds, _cornerRadius, _cornerRadius, Path.Direction.Cw); | |
_path.Close(); | |
} | |
public override void Draw(Canvas canvas) | |
{ | |
canvas.Save(); | |
canvas.ClipPath(_path); | |
base.Draw(canvas); | |
canvas.Restore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment