奇怪的胶子项目结构JavaFX的 - Android的移植 [英] Strange Gluon project structure for JavaFX - Android porting

查看:256
本文介绍了奇怪的胶子项目结构JavaFX的 - Android的移植的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

胶子插件(在Netbeans的8.0.2)对JavaFX的移植到Android的版本的Gradle创建以下目录结构:

The gradle build of Gluon plugin (in Netbeans 8.0.2) for porting JavaFX to Android creates the following directory structures:


  1. 源包[考试]

  2. 的Andr​​oid / Java程序包

  3. 桌面/ Java程序包

  4. 依奥斯/ Java程序包

每个目录都包含在他们里面的Java包。一般来说胶子的构建将创造我们的主类内的一个java包源包目录[与源包的名字包,因为它不是一个Java包,它只是一个文件系统的目录可能会产生误导。这主要类扩展JavaFX应用程序类,因而是入口点我们的应用程序。

Each of these directories contain java packages inside them. Generally Gluon build would create the "main" class for us in one java package inside "Source Packages" directory [the name Packages with "Source Packages" might be misleading since it is not a Java package, its just a file system directory]. This main class extends Javafx Application class and thus is the entry point into our application.

Android的API可以访问只在Android的/ Java进行内创建的任何Java包件目录。比方说,android.Vibrator类是只referr,能在这里。

The Android API is accessible only in Android/Java Packages directory for any java package created inside it. Say, the android.Vibrator class is only referr-able here.

问题是,我们不能提及任何Java包的Andr​​oid / Java目录内内创建一个类来源包[考试]目录中创建的任何Java包!如果是这种情况,那么,我们如何才能从javafx.Application的start()方法到发言权android.Vibrator采取应用向前发展。

The problem is, we cannot refer to a class created inside any Java package inside Android/Java directory to any java package created inside Source Packages [Java] directory!! If this is the case then how would we take the application forward from the start() method of javafx.Application into say android.Vibrator.

胶子项目结构具有快照:<一href=\"http://stackoverflow.com/questions/31422098/how-to-reference-android-jar-in-gluon-project/31425538#31425538\">How引用的android.jar在胶子工程

The gluon project structure has a snapshot at: How to reference android.jar in Gluon Project

推荐答案

如你所知,JavaFXPorts项目允许在部署桌面,Android和iOS设备的JavaFX应用程序。当它只是纯粹的JavaFX code,该项目的主要范围增加,并从那里将是在所有这些平台上看到。

As you know, JavaFXPorts project allows deploying JavaFX apps in Desktop, Android and iOS devices. When it's just pure JavaFX code, the project is added on the Main scope, and from there it will be visible in all those platforms.

只有在情况下,你需要一些特定于平台的code,你应该添加上相应的软件包。

Only in the case you need some platform specific code, you should add it on the corresponding package.

正如你所提到的,由主包默认你不会看到在一个平台上包增加code,所以你必须提供一个途径。

As you have mentioned, by default from the Main package you won't see the added code in a platform package, so you have to provide a way for that.

如果您检查JavaFXPorts库中的HelloPlatform 样品,你会发现一个 PlatformService 类使用加载包的ServiceLoader

If you check the HelloPlatform sample on JavaFXPorts repository, you will find a PlatformService class to load the packages using ServiceLoader.

另一种可能性是使用的Class.forName()在运行时动态加载的类,一旦我们知道应用程序正在运行的平台。

Another possibility is using Class.forName() to load dynamically the classes at runtime, once we know the platform where the app is running.

我建议你看一下胶子向下的项目,这为您管理的几个平台特定的服务,并为您提供统一的,独立于平台的API。

I suggest you have a look at the Gluon Down project, that manages for you several platform specific services, and provides you with uniform, platform-independent API.

有关目前无法在向下(随意贡献),则这些服务可以实现他们想在这个简单的应用程序创建的用胶子插件

For those services not available yet in Down (feel free to contribute), you can implement them like in this simple app created using Gluon Plugin.

源包[考试]

首先,创建一个 getPlatform()方法,以及提到的类添加到每个特定平台。例如,添加 org.gluonoss.vibrator.GluonAndroidPlatform.java Android上的包。

First, create a getPlatform() method, and add the referred classes to each specific platform. For instance, add org.gluonoss.vibrator.GluonAndroidPlatform.java on Android package.

public class GluonPlatformFactory {

    public static GluonPlatform getPlatform() {
        try {
            String platform = System.getProperty("javafx.platform", "desktop");
            String path = "org.gluonoss.vibrator.GluonDesktopPlatform";
            if(platform.equals("android")) {
                path = "org.gluonoss.vibrator.GluonAndroidPlatform";
            } else if(platform.equals("ios")) {
                path = "org.gluonoss.vibrator.GluonIosPlatform";
            }
            return (GluonPlatform) Class.forName(path).newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            System.out.println("Platform Error "+e.getMessage());
        }
        return null;
    }
}

现在,创建一个接口,你想在所有平台上的方法:

Now, create an interface, with the method you want on all your platforms:

public interface GluonPlatform {

    void vibrate();

}

最后,在你的主类检索平台和呼叫你的方法:

Finally, on your main class retrieve the platform and call your method:

    @Override
    public void start(Stage stage) {
        final Button button = new Button("Click me!");

        button.setOnAction(e-> GluonPlatformFactory.getPlatform().vibrate());

        StackPane root = new StackPane(button);

        Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
        Scene scene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());

        stage.setScene(scene);
        stage.show();
    }

桌面/ Java程序包

添加震动方法。现在离开它空的,但你可以添加一个时间轴动按钮,例如。

Add the vibrate method. For now leave it empty, but you could add a Timeline to move the button, for instance.

 public class GluonDesktopPlatform implements GluonPlatform {

    @Override
    public void vibrate() {
        System.out.println("Vibrating!");
    }

}

的Andr​​oid / Java程序包

添加震动方法。请注意,我们必须使用 FXActivity ,这是JavaFX的线程和Android的活动之间的桥梁。

Add the vibrate method. Notice that we have to use FXActivity, which is the bridge between the JavaFX thread and the Android activity.

public class GluonAndroidPlatform implements GluonPlatform {

    @Override
    public void vibrate() {
        Vibrator v = (Vibrator) FXActivity.getInstance().getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(500);
    }

}

不要忘记添加您AndroidManifest文件所需的权限(你会发现它在的src /安卓/ AndroidManifest.xml中

Don't forget to add the required permission on your AndroidManifest file (you will find it under src/android/AndroidManifest.xml.

现在可以部署该项目,并在桌面上( gradlew运行)运行它,它会工作,并在Android( gradlew androidInstall安装),它也可以工作。

Now you can deploy the project and run it on Desktop (gradlew run) and it will work, and install it on Android (gradlew androidInstall), and it will work too.

这篇关于奇怪的胶子项目结构JavaFX的 - Android的移植的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆