如何制作两种颜色之间带有渐变的文本?

huangapple go评论75阅读模式
英文:

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);");

如何制作两种颜色之间带有渐变的文本?

huangapple
  • 本文由 发表于 2023年6月6日 01:59:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定