我想创建一个图像滑块。我有图像视图和静态图像。如何通过转换逐渐改变图像来淡出。
答案 0 :(得分:2)
请使用我的简单下面的图片滑块代码,按钮或不使用按钮。
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ImageSliderDemo extends Application {
private List<String> list = new ArrayList<String>();
int j = 0;
double orgCliskSceneX, orgReleaseSceneX;
Button lbutton, rButton;
ImageView imageView;
@Override
public void start(Stage primaryStage) {
// images in src folder.
try {
list.add("/1.jpg");
list.add("/1.png");
list.add("/12.png");
list.add("/13.png");
list.add("/2.jpg");
list.add("/2.png");
list.add("/3.jpg");
list.add("/4.png");
list.add("/a.jpg");
list.add("/Logo.png");
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
lbutton = new Button("<");
rButton = new Button(">");
Image images[] = new Image[list.size()];
for (int i = 0; i < list.size(); i++) {
images[i] = new Image(list.get(i));
}
imageView = new ImageView(images[j]);
imageView.setCursor(Cursor.CLOSED_HAND);
imageView.setOnMousePressed(circleOnMousePressedEventHandler);
imageView.setOnMouseReleased(e -> {
orgReleaseSceneX = e.getSceneX();
if (orgCliskSceneX > orgReleaseSceneX) {
lbutton.fire();
} else {
rButton.fire();
}
});
rButton.setOnAction(e -> {
j = j + 1;
if (j == list.size()) {
j = 0;
}
imageView.setImage(images[j]);
});
lbutton.setOnAction(e -> {
j = j - 1;
if (j == 0 || j > list.size() + 1 || j == -1) {
j = list.size() - 1;
}
imageView.setImage(images[j]);
});
imageView.setFitHeight(100);
imageView.setFitWidth(300);
HBox hBox = new HBox();
hBox.setSpacing(15);
hBox.setAlignment(Pos.CENTER);
// hBox.getChildren().addAll(lbutton, imageView, rButton);
hBox.getChildren().addAll(imageView);
root.add(hBox, 1, 1);
Scene scene = new Scene(root, 800, 300);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
EventHandler<MouseEvent> circleOnMousePressedEventHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
orgCliskSceneX = t.getSceneX();
}
};
public static void main(String[] args) {
launch(args);
}
}