- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
您好,我想创建一个类似于(附加)图像的按钮。
这里的挑战是带有渐变颜色的圆角。我能够使用
创建圆角view.layer setCornerRadius:radius;view.layer setMasksToBounds:YES;
我也可以创建单一的边框颜色
view.layer.borderColor = color.CGColor; view.layer.borderWidth = 宽度;
但是渐变边框颜色需要做什么呢?
我做了以下操作,但不是我想要的。我关注了这篇文章,但它没有为我提供渐变边框颜色。 UIButton w/ gradient, rounded corners, border, and drop shadow
任何人都可以帮助我理解,我如何为 UIButton 绘制渐变边框。伪代码/示例将非常有帮助。
最佳答案
这通过创建一个在中间有一个孔的多边形圆形矩形,然后使用它来剪切线性渐变的填充,将线性渐变应用于圆形矩形周围的线条。
class DisplayView : UIView {
override func drawRect(var rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)
// Inset the rect so we can stroke it and not be outside the button bounds
rect = CGRectInset(rect, 4.0, 4.0)
// Construct the the bounds
var path = CGPathCreateWithRoundedRect(rect, rect.size.height / 2, rect.size.height / 2, nil)
// Fill the middle with white (or other background color
CGContextAddPath(context, path)
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillPath(context)
// stroke the path
path = CGPathCreateCopyByStrokingPath(path, nil, 4.0, kCGLineCapButt, kCGLineJoinBevel, 0)
// Add the outer edge of the button frame
CGContextAddPath(context, path)
// Create a gradient from white to red
let colors = [
UIColor.yellowColor().CGColor,
UIColor.redColor().CGColor
]
// Set the round rect as the clip
CGContextClip(context)
// Fill the path with the radial gradient
let baseSpace = CGColorSpaceCreateDeviceRGB();
let gradient = CGGradientCreateWithColors(baseSpace, colors, nil);
CGContextDrawLinearGradient(
context,
gradient,
CGPoint(x:0, y:0),
CGPoint(x:CGRectGetMaxX(rect), y:CGRectGetMaxY(rect)),
.allZeros)
// Fill the line
CGContextFillPath(context)
CGContextRestoreGState(context)
}
}
swift 4
class DisplayView : UIView {
override func draw(_ rect: CGRect) {
var rect = rect
let context = UIGraphicsGetCurrentContext()
context!.saveGState()
// Inset the rect so we can stroke it and not be outside the button bounds
rect = rect.insetBy(dx: 4.0, dy: 4.0)
// Construct the the bounds
var path = CGPath(roundedRect: rect,
cornerWidth: rect.size.width / 2,
cornerHeight: rect.size.height / 2,
transform: nil)
// Fill the middle with white (or other background color
context!.addPath(path)
context?.setFillColor(UIColor.clear.cgColor)
context?.fillPath()
// stroke the path
path = path.copy(strokingWithWidth: 4.0,
lineCap: CGLineCap.butt,
lineJoin: CGLineJoin.bevel,
miterLimit: 0)
// Add the outer edge of the button frame
context?.addPath(path)
// Create a gradient from white to red
let colors = [
UIColor.yellow.cgColor,
UIColor.red.cgColor
]
// Set the round rect as the clip
context?.clip()
// Fill the path with the radial gradient
let baseSpace = CGColorSpaceCreateDeviceRGB();
let gradient = CGGradient(colorsSpace: baseSpace,
colors: colors as CFArray,
locations: nil);
context?.drawLinearGradient(gradient!,
start: CGPoint(x:0, y:0),
end: CGPoint(x: rect.maxX, y: rect.maxY),
options: CGGradientDrawingOptions())
// Fill the line
context?.fillPath()
context?.restoreGState()
}
}
关于ios - 圆角渐变 UIButton iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32062415/
我有一个包含各种子元素的网格,例如网格、Stackpanel、图像...是否可以以裁剪所有内容的方式将网格的角变圆?此外,根网格的大小可能会有所不同,因此无法进行硬编码。 编辑:经过大量搜索,我发现这
我的应用程序采用非常圆润的字体设计,我希望使用勾选框作为 UI 的一部分。带有完全圆形勾选“框”的设计看起来不太好。我正在寻找圆角的标准勾选框,就像您使用容器一样。关于如何实现这一目标的任何想法。我试
我的应用程序采用非常圆润的字体设计,我希望使用勾选框作为 UI 的一部分。带有完全圆形勾选“框”的设计看起来不太好。我正在寻找圆角的标准勾选框,就像您使用容器一样。关于如何实现这一目标的任何想法。我试
所以我应该显示一个时间表,其中每个项目都有一个带有圆角的背景图像...... 我不知道如何为我的 View (当前是 TextView,但我可以更改它)提供背景(这是必须重复的图案)并为其提供圆角..
我想让 JTextArea 有一个圆角,我做了这段代码: public BPosTxtArea() { super(); setOpaque(false); } @Override p
我有两个项目,我在其中创建了一组扩展 card 类的自定义 cards,然后将它们插入到 StaggeredView 中。 我的问题是,在其中一个项目中,卡片角会自动变圆,而在另一个项目中则不会! 此
我正在寻找一些 c++ 绘图图形库来为动态键盘键创建器创建带有抗锯齿选项的圆角。我已经测试过 OpenCV 和 Magick++ 函数,但结果不是很好。谁能帮我解决这个问题? 这是一个使用 Magic
有没有办法使 QLineEdit 小部件的角变圆?如果没有,是否有类似的小部件我可以这样做? 视觉意义: 已解决:(请参阅下面的更多信息) QLineEdit *lineEdit = ne
我正在尝试创建具有圆角和背景的矩形作为重复位图。我是这样写的,但是在角落里得到了位图。 有人能帮忙吗? 背景.xml 最
我有 UIVIew 的代码: override func drawRect(rect: CGRect) { let toolbarSize = CGFloat(UIDevice.current
我正在开发一个 Windows 窗体应用程序(C#、.NET 4.0、VS 2010),其中我有一个非常标准的 MainForm 和一个 ToolStrip(GripStyle:Hidden,Dock
我设法绘制了一个矩形 :-) 但我不知道如何绘制圆角矩形。 谁能帮我解决以下如何四舍五入矩形的代码? let canvas = UIGraphicsGetCurrentContext() rec =
我有以下 SVG: 我想获得类似 CSS 的 border-top-right-radius 和 border-top-bottom-radius 效果。 如何实现
这个问题在这里已经有了答案: How to make an ImageView with rounded corners? (58 个回答) 关闭去年。 我希望图像具有圆角。我实现了这个 xml 代码
我使用此类别并为我的 UITableView 创建大小相同的图像。有没有办法让图像也有圆角?谢谢! + (UIImage *)scale:(UIImage *)image toSize:(CGSize
我对 iPhone 屏幕设计没有太多经验,但我需要制作一个像这样的表格:(图片),我进行了调查,但没有找到任何东西。该表需要有一个圆角,用户将能够插入数据,在本例中是名字、姓氏等......现在我使用
创建具有自定义背景图像的 NSButton 的最佳方法是什么,它能够具有可变的宽度,而不会使角边框看起来被拉伸(stretch)?我知道有一些方便的方法可以使用 UIButton 执行此操作:http
我知道你可以使用 .cornerRadius()将 swiftUI View 的所有角都圆角,但有没有办法只圆角特定的角,例如顶部? 最佳答案 有两个选项,您可以使用 View与 Path ,或者您可
我想在我的游戏屏幕上添加一些黑色轮廓,使其看起来像圆角。 这是我想要达到的效果: 我认为使用着色器可能很容易创建这种效果,而不是在所有内容之上绘制一个巨大的位图。 有人可以帮助我使用此效果的 GLSL
我的 NSTextField 有圆角和白色背景颜色,但 View 本身似乎是矩形的。这意味着圆角和边界矩形之间的空间显示为透明。我该如何填写该区域才能使其不突出? 图片: 代码: let textFi
我是一名优秀的程序员,十分优秀!