为了自定义我的应用程序的外观,我已经为按钮实现了自定义渲染器,在它们上面设置了渐变背景图像。到目前为止,这个功能运作良好。
然而,当我设置圆角时,边缘看起来非常模糊。我认为这可能是由于屏幕坐标和实际分辨率之间的差异造成的,但是当我应用一个因子(2或3)来适应它时,它只会完全破坏形状(仍然模糊)。
这里是一个截图,拍摄于iPhone X上(请注意条目和图像的分辨率): 这是生成背景的代码:
这是如何应用的(适用于各种不同的状态):
有人知道如何修复这个问题吗?
非常感谢!
解决方案(感谢NickSpag!):
1:在渐变层上设置适当的内容比例尺:
然而,当我设置圆角时,边缘看起来非常模糊。我认为这可能是由于屏幕坐标和实际分辨率之间的差异造成的,但是当我应用一个因子(2或3)来适应它时,它只会完全破坏形状(仍然模糊)。
这里是一个截图,拍摄于iPhone X上(请注意条目和图像的分辨率): 这是生成背景的代码:
private UIImage CreateGradientBackground(Color startColor, Color endColor)
{
var gradientLayer = new CAGradientLayer();
if (Control == null)
return null;
gradientLayer.Bounds = Control.Bounds;
gradientLayer.CornerRadius = (Control.Bounds.Width < Control.Bounds.Height) ?
Control.Bounds.Width / 2 :
Control.Bounds.Height / 2;
gradientLayer.Colors = new CGColor[] { startColor.ToCGColor(), endColor.ToCGColor() };
gradientLayer.StartPoint = new CGPoint(0, 0.5);
gradientLayer.EndPoint = new CGPoint(1, 0.5);
UIGraphics.BeginImageContext(gradientLayer.Bounds.Size);
if (UIGraphics.GetCurrentContext() == null)
return null;
gradientLayer.RenderInContext(UIGraphics.GetCurrentContext());
UIImage image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return image;
}
这是如何应用的(适用于各种不同的状态):
Control.SetBackgroundImage(gradientBackground, UIControlState.Normal);
有人知道如何修复这个问题吗?
非常感谢!
解决方案(感谢NickSpag!):
1:在渐变层上设置适当的内容比例尺:
gradientLayer.ContentsScale = UIScreen.MainScreen.Scale;
2:获取正确的图像上下文:
UIGraphics.BeginImageContextWithOptions(gradientLayer.Bounds.Size, false, UIScreen.MainScreen.Scale);
UIGraphics
图像上下文?让我们尝试将其更改为'UIGraphics.BeginImageContextWithOptions(gradientLayer.Bounds.Size, false, UIScreen.MainScreen.Scale);'
? - NickSpag