Javafx 简明教程
JavaFX - Scale Transition
正如其名称所暗示,缩放是指增大或减小对象的尺寸。在计算机图形中,通过对对象(或图像)应用比例转换,可以在特定时间内增大或减小其尺寸。
As its name suggests, scaling refers to increasing or decreasing the size of an object. In computer graphics, by applying the scale transition on an object (or image), you can either increase or decrease its size, for a specified duration.
此过渡也可以应用到各种 JavaFX 节点、2D 形状、3D 形状、文本以及应用程序上的任何其他元素。
This transition can also be applied on various JavaFX nodes, on 2D shapes, 3D shapes, text, and any other element on an application.
Scale Transition in JavaFX
比例过渡在 JavaFX 中使用 ScaleTransition 类执行,该类属于 javafx.animation 包。此类包含各种属性,用于在 JavaFX 应用程序上显示此动画 −
The Scale transition is performed in JavaFX using the ScaleTransition class which belongs to the javafx.animation package. This class contains various properties that assist in playing this animation on a JavaFX application −
-
byX − Specifies the incremented stop X scale value, from the start, of this ScaleTransition.
-
byY − Specifies the incremented stop Y scale value, from the start, of this ScaleTransition.
-
byZ − Specifies the incremented stop Z scale value, from the start, of this ScaleTransition.
-
duration − The duration of this ScaleTransition
-
fromX − Specifies the start X scale value of this ScaleTransition.
-
fromY − Specifies the start Y scale value of this ScaleTransition.
-
fromZ − Specifies the start Z scale value of this ScaleTransition.
-
node − The target node of this ScaleTransition.
-
toX − Specifies the stop X scale value of this ScaleTransition.
-
toY − The stop Y scale value of this ScaleTransition.
-
toZ − The stop Z scale value of this ScaleTransition.
Example
以下是展示 JavaFX 中缩放过渡的程序。将此代码保存在名为 ScaleTransitionExample.java 的文件中。
Following is the program which demonstrates Scale Transition in JavaFX. Save this code in a file with the name ScaleTransitionExample.java.
import javafx.animation.ScaleTransition;
import javafx.application.Application;
import static javafx.application.Application.launch;
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 ScaleTransitionExample extends Application {
@Override
public void start(Stage stage) {
//Drawing a Circle
Circle circle = new Circle();
//Setting the position of the circle
circle.setCenterX(300.0f);
circle.setCenterY(135.0f);
//Setting the radius of the circle
circle.setRadius(50.0f);
//Setting the color of the circle
circle.setFill(Color.BROWN);
//Setting the stroke width of the circle
circle.setStrokeWidth(20);
//Creating scale Transition
ScaleTransition scaleTransition = new ScaleTransition();
//Setting the duration for the transition
scaleTransition.setDuration(Duration.millis(1000));
//Setting the node for the transition
scaleTransition.setNode(circle);
//Setting the dimensions for scaling
scaleTransition.setByY(1.5);
scaleTransition.setByX(1.5);
//Setting the cycle count for the translation
scaleTransition.setCycleCount(50);
//Setting auto reverse value to true
scaleTransition.setAutoReverse(false);
//Playing the animation
scaleTransition.play();
//Creating a Group object
Group root = new Group(circle);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Scale transition example");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls ScaleTransitionExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ScaleTransitionExample
执行后,上述程序会生成如下所示的 JavaFX 窗口。
On executing, the above program generates a JavaFX window as shown below.