Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Circle Image from Xamarin Evolve
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using XamarinEvolve.Mobile.Droid.Renderers;
using XamarinEvolve.Mobile.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Graphics;
using XamarinEvolve.Mobile.Droid.Helpers;
[assembly: ExportRenderer(typeof(CircleImage), typeof(CircleImageRenderer))]
namespace XamarinEvolve.Mobile.Droid.Renderers
{
public class CircleImageRenderer : ImageRenderer
{
public CircleImageRenderer()
{
}
protected override void OnElementChanged (ElementChangedEventArgs<Image> e)
{
base.OnElementChanged (e);
if (e.OldElement == null) {
if((int)Android.OS.Build.VERSION.SdkInt < 18)
SetLayerType (LayerType.Software, null);
}
}
protected override bool DrawChild (Canvas canvas, global::Android.Views.View child, long drawingTime)
{
try{
var radius = Math.Min (Width, Height) / 2;
var strokeWidth = 10;
radius -= strokeWidth / 2;
Path path = new Path ();
path.AddCircle (Width / 2, Height / 2, radius, Path.Direction.Ccw);
canvas.Save ();
canvas.ClipPath (path);
var result = base.DrawChild (canvas, child, drawingTime);
canvas.Restore ();
path = new Path ();
path.AddCircle (Width / 2, Height / 2, radius, Path.Direction.Ccw);
var paint = new Paint ();
paint.AntiAlias = true;
paint.StrokeWidth = 5;
paint.SetStyle (Paint.Style.Stroke);
paint.Color = global::Android.Graphics.Color.White;
canvas.DrawPath (path, paint);
paint.Dispose();
path.Dispose();
return result;
}catch(Exception ex) {
}
return base.DrawChild (canvas, child, drawingTime);
}
}
}
using System;
using System.ComponentModel;
using XamarinEvolve.Mobile.Controls;
using XamarinEvolve.Mobile.iOS.Renderers;
using MonoTouch.CoreGraphics;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof (CircleImage), typeof (CircleImageRenderer))]
namespace XamarinEvolve.Mobile.iOS.Renderers
{
public class CircleImageRenderer : ImageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
return;
try{
double min = Math.Min(Element.Width, Element.Height);
Control.Layer.CornerRadius = (float) (min/2.0);
Control.Layer.MasksToBounds = false;
Control.Layer.BorderColor = new CGColor(1, 1, 1);
Control.Layer.BorderWidth = 3;
Control.ClipsToBounds = true;
}catch(Exception ex){
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == VisualElement.HeightProperty.PropertyName ||
e.PropertyName == VisualElement.WidthProperty.PropertyName)
{
try{
double min = Math.Min(Element.Width, Element.Height);
Control.Layer.CornerRadius = (float) (min/2.0);
Control.Layer.MasksToBounds = false;
Control.ClipsToBounds = true;
}
catch(Exception ex){
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XamarinEvolve.Mobile.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.WinPhone;
using XamarinEvolve.Mobile.WinPhone.Renderers;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Imaging;
[assembly: ExportRenderer(typeof(CircleImage), typeof(CircleImageRenderer))]
namespace XamarinEvolve.Mobile.WinPhone.Renderers
{
public class CircleImageRenderer : ImageRenderer
{
public CircleImageRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || this.Element == null)
return;
/*Control.Source = new BitmapImage(new
Uri("/Assets/missing.png", UriKind.Relative)); */
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if(Control != null && Control.Clip == null)
{
var min = Math.Min(Element.Width, Element.Height) / 2.0f;
if (min <= 0)
return;
Control.Clip = new EllipseGeometry
{
Center = new System.Windows.Point(min, min),
RadiusX = min,
RadiusY = min
};
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment