本文概述
可以对节点的笔触颜色进行动画处理, 以使笔触颜色可以在指定的持续时间内在两个颜色值之间波动。
在JavaFX中, 类javafx.animation.FillTransition表示填充过渡。我们需要实例化此类以创建适当的Fill Transition。
属性
下表描述了该类的属性以及setter方法。
属性 | 描述 | 设置方法 |
---|---|---|
duration | 这是Duration类的对象类型属性。它代表笔画过渡的持续时间。 | setDuration(Duration duration) |
fromValue | 这是一种颜色类型属性。它代表笔画过渡的颜色初始值。 | setFromValue(Color value) |
shape | 这是Shape类的对象类型属性。它代表将在其上应用笔触过渡的形状。 | setShape(Shape shape) |
toValue | 这是颜色类型属性。它代表笔划过渡的颜色目标值。 | setToValue(Color value) |
构造函数
该类中有五个构造函数。
- public StokeTransition():使用默认参数创建StrokeTransition的新实例。
- public StokeTransition(Duration duration):使用指定的持续时间值创建Stroke Transition的新实例
- 公共StokeTransition(持续时间, Color fromValue, Color toValue):使用指定的持续时间, 颜色的初始值和颜色的目标值创建StrokeTransition的新实例。
- public StokeTransition(Duration duration, Shape shape):使用指定的持续时间和要应用过渡的形状创建StrokeTransition的新实例。
- 5public StokeTransition(持续时间, 形状, 颜色fromValue, Color toValue):使用指定的持续时间, 形状, 颜色的初始值和颜色的目标值创建StrokeTransition的新实例。
例
在以下示例中, 圆圈的笔划从黑色变为紫色。
package application;
import javafx.animation.StrokeTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Stroke_Transition extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Creating Circle
Circle cir = new Circle(200, 150, 100);
//Setting stroke and color for the circle
cir.setStroke(Color.BLUE);
cir.setFill(Color.RED);
cir.setStrokeWidth(10);
//Instantiating StrokeTransition class
StrokeTransition stroke = new StrokeTransition();
//The transition will set to be auto reserved by setting this to true
stroke.setAutoReverse(true);
//setting cycle count for the Stroke transition
stroke.setCycleCount(500);
//setting duration for the Stroke Transition
stroke.setDuration(Duration.millis(500));
//setting the Initial from value of the Stroke color
stroke.setFromValue(Color.BLACK);
//setting the target value of the Stroke color
stroke.setToValue(Color.PURPLE);
//setting polygon as the shape onto which the Stroke transition will be applied
stroke.setShape(cir);
//playing the Stroke transition
stroke.play();
//Configuring Group and Scene
Group root = new Group();
root.getChildren().addAll(cir);
Scene scene = new Scene(root, 420, 300, Color.WHEAT);
primaryStage.setScene(scene);
primaryStage.setTitle("Stroke Transition example");
primaryStage.show();
}
}
输出:
评论前必须登录!
注册