- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是两张 PNG 图像:
从视觉上看,它们完全相同 - 唯一的区别是某些像素具有半透明背景(您可以下载图像来检查)。
但是当我在 JavaFX 节点上使用这些图像作为图像光标时,我得到以下结果:
第一个光标(没有部分透明的像素)仍然清晰,但第二个光标扭曲。
在与这个问题斗争了一段时间后,我发现了解释这种差异的算法 - 混合模式:
“预期”方式(例如,您可以在此浏览器中看到)是获取每个 channel 的值总和,并按 alpha 值加权:(1 - alpha) * background_color + alpha *前景颜色
。
“JavaFX Cursor”给出了不同的公式:(1 - alpha) * background_color + alpha^2 * foreground_color
(注意正方形)。
我发现了扭曲,但我不知道我做错了什么以及如何纠正这个问题。
这是我的测试程序的完整可运行源代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.ImageCursor;
import javafx.scene.image.Image;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
System.out.println(ImageCursor.getBestSize(32, 32));
primaryStage.setTitle("Hello World!");
StackPane root = new StackPane();
root.setCursor(new ImageCursor(new Image("/test-cursor.png"), 0, 0));
primaryStage.setScene(new Scene(root, 100, 100));
primaryStage.show();
}
}
如何实现这种半透明光标的正确渲染?
最佳答案
更新:经过更深入的检查,似乎 JavaFX 没有错误 - 错误似乎出在视频驱动程序实现中。下面的代码确实适用于某些硬件、驱动程序和操作系统的组合 - 但不适用于所有组合。
不幸的是,目前最好的解决方案似乎是避免使用具有部分透明的白色或灰色像素的光标。不过,部分透明的黑色像素就可以了。
<小时/>我找到了解决该问题的方法(在 JDK 8 和 Linux&Windows 上测试)。它很丑陋,需要反射(reflection),但似乎有效。下面的代码(使用 Scala 语法,但可以轻松适应 Java):
import com.sun.prism.PixelFormat
import javafx.scene.ImageCursor
import javafx.scene.image.{Image, WritableImage}
private def undoPremultipliedAlpha(image: Image): Image = {
// Fixes JavaFX bug with semi-transparent cursors -
// somewhere deep in JavaFX code they premultiply alpha
// on already premultiplied image, which screws up transparencies.
// This method attempts to counteract it by removing premultiplied alpha
// directly from bytes of internal JavaFX image.
def getPlatformImage(image: Image) = image.impl_getPlatformImage()
val platformImage = getPlatformImage(image)
val pixelFormat = platformImage.getClass.getDeclaredMethod("getPixelFormat").invoke(platformImage).asInstanceOf[PixelFormat]
if (pixelFormat != PixelFormat.BYTE_BGRA_PRE) {
println(s"wrong platform image pixel format (${pixelFormat}), unable to apply cursor transparency bug workaround")
} else {
val pixelBufferField = platformImage.getClass.getDeclaredField("pixelBuffer")
pixelBufferField.setAccessible(true)
val pixelBuffer = pixelBufferField.get(platformImage).asInstanceOf[java.nio.Buffer]
val pixelArray = pixelBuffer.array().asInstanceOf[Array[Byte]]
for (i <- 0 until pixelArray.length / 4) {
val alpha = (pixelArray(i * 4 + 3).toInt & 0xff) / 255.0
if (alpha != 0) {
pixelArray(i * 4) = math.min(255, math.max(0, ((pixelArray(i * 4).toInt & 0xff).toDouble / alpha))).toInt.toByte
pixelArray(i * 4 + 1) = math.min(255, math.max(0, ((pixelArray(i * 4 + 1).toInt & 0xff).toDouble / alpha))).toInt.toByte
pixelArray(i * 4 + 2) = math.min(255, math.max(0, ((pixelArray(i * 4 + 2).toInt & 0xff).toDouble / alpha))).toInt.toByte
}
}
}
image
}
def createImageCursor(resource: String, hotspotX: Int, hotspotY: Int): ImageCursor = {
new ImageCursor(
undoPremultipliedAlpha(
new Image(resource)),
hotspotX,
hotspotY
)
}
关于java - 为什么 javafx 会破坏我的半透明光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46426283/
我在 NSWindow 中有一个 NSView,NSWindow 是标准的 Mac 灰色/米色。 我想让 NSView 稍微半透明和灰色(例如 alpha 为 0.2 的灰色),以便 NSWindow
我对 openGL 中的 alpha 混合有疑问... 我尝试了一些绘制透明对象的方法...通过在绘制透明面之前禁用 GL_DEPTH_TEST 并在绘制透明面后再次重新启用 GL_DEPTH_TES
我将导航栏自定义为清除。所以我将backgroundImage设置为UIImage()并且isTranslucent = true。所以我遇到的问题是:这就是我在 xib 中设置的。现在 TopCon
我一直在研究这个,似乎唯一的方法是使用 UIActionSheet,问题是我的 UIActionSheet 只覆盖了一半的屏幕,我的代码是: MyViewController *myVC = [[my
我希望在我的主要 Activity 中使用半透明的状态栏和导航栏,而所有其他 Activity 都使用 Material 设计。 到目前为止我得到的是: if (Build.VERSION.SDK_I
我可以在不使用私有(private)方法等的情况下获得对 UIAlertView 的 backgroundImageView 的引用。但是我的 alpha 设置为 1.0 被忽略了。控件仍然有点半透明
我正在研究 UIWebView,我想要的是 webView 不要过度说唱 View 。 所以这就是问题所在。我设置了 UIWebView ,状态栏覆盖在内容上。 我该如何处理?我在制作 UIImage
我正在为一款小型 2d 游戏在其他图像之上绘制半透明图像。目前要混合图像,我正在使用此处找到的公式:https://en.wikipedia.org/wiki/Alpha_compositing#Al
我正在尝试将 Splash Screen 4 制作成 Win 应用程序。 我的设置: 表单边框样式设置为无。起始位置是屏幕中心。表单的背景图像设置为 PNG 文件,具有圆形边缘和“内置”投影。 在我设
(此问题发生在使用 Java 1.7.0_45 的 Mac OS X 10.10 上。在使用 Java 1.7.0_55 的 Windows 7 上我没有遇到此问题。) 我有一个半透明的“始终在最前面
如果您知道学习如何使用 Adobe Flash 为 iPhone 应用程序甚至 Air 应用程序创建此类效果的任何良好起点,那将会有很大帮助。 具体来说,我希望它不保存叠加层,只在用户从相机拍摄图
我有以下行: label.setBackground(new java.awt.Color(0, 150, 0, 50)); 我将它放在 MouseAdapter 中的 mouseRe
我正在制作幻灯片。我希望导航栏看起来与照片应用程序中的一样。我如何获得这种透明度? 我试过: - (void)drawRect:(CGRect)rect { [[UIColor clearCo
我试图在半透明的 div 上获取文本。这是在 Chrome 中,如果相关(即 Chrome 错误)。出于某种原因,每当文本位于 div 顶部(移动到负边距顶部)时,它也是部分透明的。 HTML 如下(
我在 NSVisualEffectView 上有 2 个 NSButton。第一个按钮图像是系统 NSEnterFullScreenTemplate 图像。第一个按钮上的图像是半透明的。 在第二个按钮
我想在RelativeLayout中创建一个RelativeLayout半透明。 它是页脚的项目。它始终可见,但我只想使其黑色的透明度为 50%。 我尝试过使用#11000000 Alpha chan
我正在尝试使用 Qt (C++) 应用程序跨操作系统完成以下任务。运行该程序后,会弹出一个新窗口,一个全屏 QWidget。现在我希望它是透明的/透明的,这样用户就不会真正看到它。在这个“层”上,用户
所以,我的目标是绘制具有半透明像素的 Sprite 。 首先,我使用仅渲染不透明像素的着色器渲染 Sprite 。然后,我禁用深度缓冲区写入并使用仅渲染半透明(或透明 Sprite )的着色器渲染相同
CSS .popup { z-index: 10000; filter: alpha(opacity=40); -moz-opacity: .40; backgroun
我正在尝试创建一个 lightbox effect在我的应用程序中。为实现这一点,我有一个 UserControl,其中一个面板代表半透明覆盖层,另一个面板托管所有必要的内容。 当我显示此 UserC
我是一名优秀的程序员,十分优秀!