问题描述
我有一个 javafx.scene.image.Image 类的接口.我必须实现这一点并创建 ImageView.那么,如何将 JavaFx 添加到我的 Android Studio 项目中以及如何在我的活动中执行 ImageView 或其他 Image 元素?
用户.java
import javafx.scene.image.Image;公共接口用户{字符串 getName();int getId();图像 getIcon();}
基于
2.修改构建gradle文件
build.gradle(顶级项目)
buildscript {存储库{jcenter()}依赖{类路径 'me.tatarka:gradle-retrolambda:3.5.0'类路径 'com.android.tools.build:gradle:2.2.3'}}所有项目{存储库{jcenter()}}任务清理(类型:删除){删除 rootProject.buildDir}
build.gradle(模块:app)
在这个文件中,我们将包含 dalvik-sdk
,Android 的 JavaFXPorts 依赖项.如果您使用 Java IDE 的 Gluon 插件通过 jfxmobile 插件创建移动项目,您可以找到它安装在:
~/.gradle/caches/modules-2/files-2.1/org.javafxports/dalvik-sdk/8.60.10/e...a/unpacked/dalvik-sdk
否则,可以从此处下载.将它提取到给定的路径,比如 $dalvikPath
.
应用插件:'com.android.application'安卓 {compileSdkVersion 26构建工具版本26.0.3"默认配置{applicationId "gluonhq.com.myfragment"minSdk 版本 25目标SDK版本26版本代码 1版本名称1.0"multiDexEnabled truetestInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}dexOptions {preDexLibraries = falseadditionalParameters=['--core-library']}构建类型{释放 {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}源集{主要的 {jniLibs.srcDir 文件("$dalvikPath/rt/lib")assets.srcDirs = ['assets']}}lintOptions {abortOnError false}}依赖{编译文件树(目录:'libs',包括:['*.jar'])编译文件树(包括:['*.jar'],目录:'$dalvikPath/rt/lib/ext')androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {排除组:'com.android.support',模块:'support-annotations'})编译'com.android.support:multidex:1.0.1'编译'com.android.support:appcompat-v7:26.0.0-alpha1'编译'com.android.support:design:26.0.0-alpha1'testCompile 'junit:junit:4.12'}
3.创建一个扩展 FXFragment
在MainActivity.java
中:
public static class PlaceholderFragment extends FXFragment {公共占位符片段(){String fxapp = "your.package.myfragment.MainFX";this.setName(fxapp);}@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}}
并从 onCreate
调用它:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);如果(savedInstanceState == null){getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).犯罪();}...}
请注意,activity_main.xml
中需要 android:id="@+id/container"
.
4.最后一步:创建 JavaFX 类
确保包名是your.package.myfragment
.
MainFX.java
public class MainFX extends Application {@覆盖公共无效开始(阶段阶段)抛出异常{最终 ImageView imageView = new ImageView();imageView.setImage(new Image("https://upload.wikimedia.org/wikipedia/en/c/cc/JavaFX_Logo.png"));imageView.setPreserveRatio(true);屏幕主屏幕 = Screen.getPrimary();Rectangle2D visualBounds = primaryScreen.getVisualBounds();双倍宽度 = visualBounds.getWidth();双倍高度 = visualBounds.getHeight();StackPane stackPane = new StackPane(imageView);场景场景 = 新场景(stackPane, width, height);imageView.fitWidthProperty().bind(scene.widthProperty().subtract(20));stage.setScene(场景);舞台表演();}}
请注意,这是一个纯 JavaFX 应用程序类,您需要根据屏幕边界设置舞台大小.
部署和运行应用
请注意,您将在 Android 视图中运行 JavaFX,但根据您的布局,您仍然有其他 Android 组件位于不同的视图之上.
I have an interface with javafx.scene.image.Image class. I must implement this and create ImageView. So, how to add JavaFx into my Android Studio Project and how to do ImageView or other Image element in my activity?
User.java
import javafx.scene.image.Image;
public interface User
{
String getName();
int getId();
Image getIcon();
}
Based on JavaFXPorts and the Kokos sample, you can add JavaFX to an Android project created with Android Studio, and you will be able to run a JavaFX scene inside an Android fragment.
Note that JavaFXPorts is initially intended to run full JavaFX projects on Android and iOS, using plain Java, without native code. Doing it the other way around is possible, but it is not the best approach.
Anyway, these are the required steps to create a JavaFX scene with a JavaFX ImageView
to hold a JavaFX Image
.
1. Create a simple project on Android Studio
Select a Basic Activity template, and select the use of a fragment.
2. Modify the build gradle files
build.gradle (top level project)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.5.0'
classpath 'com.android.tools.build:gradle:2.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle (module: app)
In this file we'll include dalvik-sdk
, the JavaFXPorts dependency for Android. If you use the Gluon plugin for your Java IDE to create mobile projects with the jfxmobile plugin, you can find it installed in:
~/.gradle/caches/modules-2/files-2.1/org.javafxports/dalvik-sdk/8.60.10/e...a/unpacked/dalvik-sdk
Else, it can be downloaded from here. Extract it to a given path, let's say $dalvikPath
.
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.3"
defaultConfig {
applicationId "gluonhq.com.myfragment"
minSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dexOptions {
preDexLibraries = false
additionalParameters=['--core-library']
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDir file("$dalvikPath/rt/lib")
assets.srcDirs = ['assets']
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: '$dalvikPath/rt/lib/ext')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support:design:26.0.0-alpha1'
testCompile 'junit:junit:4.12'
}
3. Create a class that extends FXFragment
In MainActivity.java
:
public static class PlaceholderFragment extends FXFragment {
public PlaceholderFragment() {
String fxapp = "your.package.myfragment.MainFX";
this.setName(fxapp);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
and call it from onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
...
}
Note that android:id="@+id/container"
is required in activity_main.xml
.
4. Final step: create the JavaFX class
Make sure the package name is your.package.myfragment
.
MainFX.java
public class MainFX extends Application {
@Override
public void start(Stage stage) throws Exception {
final ImageView imageView = new ImageView();
imageView.setImage(new Image("https://upload.wikimedia.org/wikipedia/en/c/cc/JavaFX_Logo.png"));
imageView.setPreserveRatio(true);
Screen primaryScreen = Screen.getPrimary();
Rectangle2D visualBounds = primaryScreen.getVisualBounds();
double width = visualBounds.getWidth();
double height = visualBounds.getHeight();
StackPane stackPane = new StackPane(imageView);
Scene scene = new Scene(stackPane, width, height);
imageView.fitWidthProperty().bind(scene.widthProperty().subtract(20));
stage.setScene(scene);
stage.show();
}
}
Note that this is a pure JavaFX Application class, and that you need to set the stage size base on the screen bounds.
Deploy and run the app
Note that you will have JavaFX running in an Android view, but you still have other Android components on different views on top of it, depending on your layout.
这篇关于如何在 Android Studio 中运行 JavaFX 并制作 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!