英文:
How to make text with gradient between two colors?
问题
我想重新创建一个效果,其中文本的两个极端边缘有一种颜色,而中间有另一种颜色。
我唯一知道的方法是使用setFill,但显然它只填充文本为单一颜色。
英文:
I would like to recreate an effect where there is one color on the two extreme sides of the text and another in the center.
The only method I know of is setFill but obviously it fills the text with a single color.
答案1
得分: 3
setFill(...)
显然是用单一颜色填充文本。
不清楚您从何处获取这些信息。根据文档,setFill()
接受任何类型的Paint
对象,包括线性渐变和径向渐变;它不仅限于Color
实例。
只需使用适当的渐变调用setFill(...)
。
英文:
> setFill(...)
... obviously it fills the text with a single color
It is not clear where you get that information from. According to the documentation, setFill()
takes any kind of Paint
object, which would include both linear gradients and radial gradients; it is not limited to Color
instances.
Just use setFill(...)
with an appropriate gradient.
答案2
得分: 1
这只是为了展示@James_D已经提到的内容。
你可以将线性渐变和径向渐变都应用到文本上。但你需要记住,在Text
上的线性渐变与径向渐变之间的差异不会像在普通的Region
/Rectangle
上那么有效。它们看起来会有点类似,但有一些差异。所以你需要调整径向渐变的参数以获得期望的效果。
Text text1 = new Text("Resourceful");
text1.setStyle("-fx-font-size: 50px; -fx-fill:linear-gradient(to right, red, blue, red);");
Text text2 = new Text("Resourceful");
text2.setStyle("-fx-font-size: 50px; -fx-fill:radial-gradient(focus-distance 0% , center 50% 50% , radius 50% , blue, red);");
英文:
This is to just show you what @James_D already mentioned.
You can set both linear & radial gradients to the text. But you need to keep in mind that the difference in linear vs radial gradients on Text
will not be as effective as on the normal Region
/Rectangle
. They will look like similar with a little difference. So you need to play with the radial gradient parameters for desired effect.
Text text1 = new Text("Resourceful");
text1.setStyle("-fx-font-size: 50px; -fx-fill:linear-gradient(to right, red, blue, red);");
Text text2 = new Text("Resourceful");
text2.setStyle("-fx-font-size: 50px; -fx-fill:radial-gradient(focus-distance 0% , center 50% 50% , radius 50% , blue, red);");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论