【问题标题】:Gluon application got problem when MousePressed fast click or double, tripple click当 MousePressed 快速单击或双击、三次单击时,Gluon 应用程序出现问题
【发布时间】:2019-05-28 14:52:26
【问题描述】:

我已经开发了使用uuid 生成条形码和二维码的胶子应用程序。使用可以根据商店在条形码和二维码之间切换。当使用点击 QRCODE(图像)时,条码将仅在纵向屏幕模式下显示。所以当用户点击图片(条形码/二维码)时,屏幕会自动旋转。

this.img.setOnMousePressed(event -> {
//Rotate Screen
        Services.get( CMOrientationService.class ).ifPresent( o -> {
            Orientation orientation = Services.get( OrientationService.class )
                    .flatMap(OrientationService::getOrientation)
                    .orElse(Orientation.HORIZONTAL);

            Services.get(LogService.class).ifPresent(service -> service.log("orientation="+orientation.toString()));

            if (orientation == Orientation.VERTICAL) {
                Services.get(LogService.class).ifPresent(service -> service.log("Currently="+orientation.toString()));
                //Change to Barcode
                //GenerateBarQRCode(orientation == Orientation.VERTICAL);
                o.coerceOrientation( Orientation.HORIZONTAL );
            } else {
                Services.get(LogService.class).ifPresent(service -> service.log("Currently="+orientation.toString()));
                //Change to QRCode
                //GenerateBarQRCode(orientation == Orientation.VERTICAL);
                o.coerceOrientation( Orientation.VERTICAL );
            }
            GenerateBarQRCode(orientation == Orientation.VERTICAL);
        } );
});

当用户尝试快速点击图片(双击、三次点击)时会出现问题。

您可以查看this video 以获得更多了解(请看视频中的 6 秒)。

注意。它仅在 Android 中出错。

【问题讨论】:

  • 这可能是同步问题,你可以尝试同步 onMousePressed 事件中的代码,这样它就可以处理多个触摸。您还可以使用在处理时未设置并在处理完成时设置的原子标志,即在 GenerateBarQRCode 方法之后,在未设置标志时不应执行代码。使用它不需要的触摸将被消耗而不是执行。
  • 你能给我一些代码吗?
  • 不确定这是否可行,但您可以检查 the clickCount property of the MouseEvent 并仅在为 1 时触发更改。如果这不起作用,请考虑存储最后一次更改方向的时间由此事件处理程序触发,并确保仅在自上次方向更改后经过一定时间时才执行逻辑。
  • @fabian 当我在桌面上使用 clickCount 时,它运行良好。但是在Android中clickCount总是1,所以如果我们双击,这个事件会触发两次。
  • @fabian:是的,如果我们延迟到 1000 毫秒,它会起作用。但它会影响性能,当应用程序延迟时会显示黑屏。所以这个解决方案有效,但我认为使用它不是一个好主意。

标签: javafx gluon gluon-mobile


【解决方案1】:

你可以像这样使用同步

// At class level
private Object mutex;
//initialize this object in constructor like this
mutex = this;

this.img.setOnMousePressed(event -> {
//Rotate Screen
synchronized(mutex){
// your code here
       ....
}
});

然而,在上述实例中,多个点击事件将以串行方式执行。如果您希望在执行之前忽略多次点击,那么您可以使用以下代码 sn-p

// At class level
private boolean mutex = true;

this.img.setOnMousePressed(event -> {
   if(mutex){
     mutex = false;

        // your code here
               ....
     mutex = true;
   }
});

【讨论】:

  • 您好感谢您的回答,但它不起作用。问题依然存在
  • JavaFX 事件都在单个线程上执行。这不是并发线程的问题。如果是这样,则不能保证第二个实现在线程之间传达mutex 字段的更改...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
相关资源
最近更新 更多