2
我这里使用的代码有一个形象的渲染:https://blog.xamarin.com/elegant-circle-images-in-xamarin-forms/圈的图像不是正圆
public class ImageCircleRenderer: ImageRenderer
{
private void CreateCircle()
{
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)
{
Debug.WriteLine("Unable to create circle image: " + ex);
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
CreateCircle();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == VisualElement.HeightProperty.PropertyName ||
e.PropertyName == VisualElement.WidthProperty.PropertyName)
{
CreateCircle();
}
}
}
然后,我把我的表视图,包含照片的观察室。
像这样:
private void SetTableView()
{
int photoSize = 120;
var photo = new ImageCircle
{
Source = "profile_placeholder.png",
WidthRequest = photoSize,
HeightRequest = photoSize,
Aspect = Aspect.AspectFill,
HorizontalOptions = LayoutOptions.Center,
Scale = 1.0
};
Content = new TableView
{
HasUnevenRows = true,
Intent = TableIntent.Menu,
Root = new TableRoot()
{
new TableSection()
{
new ViewCell
{
Height = photoSize,
View = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.Start,
VerticalOptions = LayoutOptions.Start,
Padding = 15,
Children =
{
photo,
new Label
{
Text = "Casa de Férias"
}
}
}
}
}
}
};
}
不幸的是,结果是这样的:
我怎样才能让这幅画一个完美的圆?我看不出唉原因为什么这是行不通的......
我设置相同的宽度和高度,并应填写作为图像的纵横属性定义的可用空间,同时保持长宽比。
我错过了什么?
谢谢!
编辑:我尝试使用ImageCircle插件詹姆斯Montemagno,同样的事情发生了。我猜这可能是我的布局有问题?
这只能与方形图像。在您链接到的网站上,它表示:“这里的关键是确保您为图片请求相同的宽度和高度,并将方面设置为AspectFill,以确保完美的方形可以减少您的圈子。” – Meyer
图像占位符实际上是一个完美的广场。我使用的是120x120图片,然后是240x240的2x资源和360x360的3x资源。 – nmdias
你说得对,对不起。我看到你已经发现了这个问题。 – Meyer