java 将 AWT 图形代码移植到 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5783624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Porting AWT graphics code to Android
提问by Tony the Pony
We would like to use some of our existing Java AWT graphics code on the Android platform. As far as I can tell, Android does not include any of the AWT classes -- no Graphics2D
, Path2D
, FontMetrics
, etc.
我们想在 Android 平台上使用我们现有的一些 Java AWT 图形代码。据我所知,Android不包括任何AWT类-不Graphics2D
,Path2D
,FontMetrics
,等。
What is the best approach to port our drawing code to Android? Ideally, we would like to modify our code base to target bothAndroid and generic Java.
将我们的绘图代码移植到 Android 的最佳方法是什么?理想情况下,我们要修改我们的代码库为目标既Android和一般的Java。
采纳答案by farm ostrich
The android platform supports a small subset of awt. By small, I mean it supports awt fonts. Going from java swing (are you really just using awt as a standalone UI?) to Android is going to be a shock to the system. One defines Android's UI in XML resource files, and those resources are loaded into Activity classes which represents a logical unit of the application. Canvas' replace Graphics2D objects, and they have somewhat different functionality.The Android UI system seeks to avoid absolute positioning, which is common in java UI's. And there is so much more to consider for Android, like varying screen sizes and differences in resolution. Neither of which were much of a issue for Java SE. To answer your question: you have your work cut out for you and you will have to start much of your coding from scratch.
android平台支持awt的一小部分。小的,我的意思是它支持 awt 字体。从 java swing(你真的只是使用 awt 作为一个独立的 UI 吗?)到 Android 将会对系统造成冲击。一个在 XML 资源文件中定义 Android 的 UI,这些资源被加载到 Activity 类中,该类代表应用程序的一个逻辑单元。Canvas 取代了 Graphics2D 对象,它们的功能有些不同。Android UI 系统力求避免绝对定位,这在 java UI 中很常见。对于 Android,还有很多需要考虑的因素,例如不同的屏幕尺寸和分辨率的差异。对于 Java SE 而言,这两者都不是什么大问题。回答您的问题:您的工作已为您完成,您将不得不从头开始编写大部分代码。
I'm not sure what 'drawing code' means, but in my case, I have a large amount of animation code that is displayed via awt shapes. This is reproducable on android as there are graphics.drawable.shapes objects, and one can display primitive shapes by doing something like canvas.drawCircle(x,y,z,h) (remind you of something?). But when I ported it, it was difficulty and felt like I was cortorting the android to do something it really didn't want to, and wasn't intended to do. It's hard to answer your question exactly given the vagueness of it.
我不确定“绘图代码”是什么意思,但就我而言,我有大量通过 awt 形状显示的动画代码。这是可在 android 上重现的,因为有 graphics.drawable.shapes 对象,并且可以通过执行诸如 canvas.drawCircle(x,y,z,h) 之类的操作来显示原始形状(提醒您什么?)。但是当我移植它时,它很困难,感觉就像我在扭曲机器人做一些它真的不想做的事情,也不打算做。鉴于问题的含糊性,很难准确回答您的问题。
回答by Vicente Plata
I don't think you can port AWT to Android. I mean, it's Java SE vs a subset of it (or an updated Java ME with steroids, as you prefer).
我认为您无法将 AWT 移植到 Android。我的意思是,它是 Java SE 与它的一个子集(或更新的带有类固醇的 Java ME,如您所愿)。
There are though, some UI classes for Java ME that work perfectly on Android. Personally I like LWUIT but it's not the only one.
不过,有一些适用于 Java ME 的 UI 类可以在 Android 上完美运行。我个人喜欢 LWUIT,但它不是唯一的。
回答by bth_
appengine-awt is a pure java implementation of the java.awt and javax.imageio packages for use in the Google AppEngine environment.
appengine-awt 是在 Google AppEngine 环境中使用的 java.awt 和 javax.imageio 包的纯 Java 实现。
https://github.com/witwall/appengine-awt
https://github.com/witwall/appengine-awt
SwingWT is a 100% pure Java library which aims to be a free implementation of Swing and AWT. Unlike Swing, it drives native peer widgets for your platform from SWT.
SwingWT 是一个 100% 纯 Java 库,旨在成为 Swing 和 AWT 的免费实现。与 Swing 不同的是,它从 SWT 为您的平台驱动本地对等小部件。