[英]JavaFX Canvas draw image with transparency
With a JavaFX Canvas, you can use drawImage()
. 使用JavaFX Canvas,您可以使用
drawImage()
。 However, is there anyway to draw the image with transparency (draw it with only 50% of opacity) or tint it with color? 但是,无论如何都要用透明度绘制图像(仅用50%的不透明度绘制)或用颜色着色?
Methods to Control Canvas Draw Operations 控制画布绘制操作的方法
There are methods to control the attributes of draw canvas drawing operations: 有一些方法可以控制绘制画布绘制操作的属性:
Sample Usage 样本用法
The source image used by the program is: 该程序使用的源图像是:
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.effect.*;
import javafx.scene.image.Image;
import javafx.scene.paint.*;
import javafx.stage.Stage;
public class CanvasEffects extends Application {
@Override
public void start(Stage stage) {
final Image image = new Image(IMAGE_LOC);
final int NUM_IMGS = 5;
final double W = image.getWidth();
final double H = image.getHeight();
final Canvas canvas = new Canvas(W * NUM_IMGS, H);
final GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.GOLD);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setGlobalBlendMode(BlendMode.SCREEN);
for (int i = 0 ; i < NUM_IMGS; i++) {
final double opacity = 1 - ((double) i) / NUM_IMGS;
System.out.println(opacity);
gc.setGlobalAlpha(opacity);
gc.setEffect(new BoxBlur(i * 2, i * 2, 3));
gc.drawImage(image, i * W, 0);
}
stage.setScene(new Scene(new Group(canvas)));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
// icon license: Linkware (Backlink to http://uiconstock.com required) commercial usage not allowed.
private static final String IMAGE_LOC = "http://icons.iconarchive.com/icons/uiconstock/flat-halloween/128/Halloween-Bat-icon.png";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.