黑科技:使用AI和機器學習將Android項目秒變IOS項目
- 2019 年 11 月 13 日
- 筆記
最近看Github,發現了一個剛出爐的黑科技:利用人工智慧(AI)和機器學習(ML)技術將現有Android源程式碼轉換成iOS程式碼。目前支援Android項目中的資產目錄文件和UI布局轉換,以及部分外部庫的轉換。有了這個神器,以後一個人就可以輕鬆搞定兩個平台了,真是爽歪歪啊。。。。我根本不需要學習iOS了。。。
已支援的功能:
- Android的資產目錄 (如:
jpg,png,.9圖等) => "Assets.xcassets" - mipmap目錄的圖片資源 => 「.appiconset」
- Color形式的xml => 「.colorset」
- Shape形式的xml => PDF format in asset catalog
- Vector形式的xml文件 => Swift Code (CAShapeLayer)
- Animation形式的xml文件
- UI布局的xml文件 => storyboard file
- Java的POJO類 => Swift
- 外部的庫Glide的轉換
正在開發中的功能:
- 資源(Resource)xml文件
- Android SDK的類
- 更多的外部的庫,比如:GSON, Retrofit2, ButterKnife等。
- 矢量圖動畫(Vector Animation)xml資源
未來支援的功能:
- Android的Kotlin語言
- Android的JetPack
- Android的程式碼編寫的布局 轉成 SwiftUI
- Lagacy .storyboard 轉成 SwiftUI
- 測試用例的生成
UI的轉換
目前支援CardView,Switch,ImageButton,ToggleButton這些控制項類型的轉換。
部分效果圖如下:
CardView的螢幕截圖:

image
開關,ImageButton,ToggleButton的螢幕截圖:

image
Button的螢幕截圖:

image
TextView的螢幕截圖:

image
使用普通鍵盤的EditText的螢幕截圖:

image
帶電話撥號鍵盤的EditText的螢幕截圖:

image
布局的轉換
目前支援RelativeLayout,FrameLayout,LinearLayout (Nested),ConstraintLayout,TableLayout和ScrollView這些布局類型的轉換。另外只有部分布局屬性做了轉換,如下表所示:
Control | Attributes |
|---|---|
LinearLayout | orientation, gravity, background, textAppearance |
RelativeLayout | layout_centerHorizontal, layout_centerVertical, layout_toLeftOf, layout_toRightOf, layout_above, layout_below, layout_alignParentTop, layout_alignParentBottom, layout_alignParentRight, layout_alignParentLeft |
FrameLayout | layout_marginTop, layout_gravity = right, left, right+bottom, left+bottom, center, center_horizontal, center_vertical, right+center_vertical, bottom+center_horizontal |
ConstraintLayout | Bottom_toBottomOf, Top_toTopOf, Top_toBottomOf, Left_toLeftOf, Start_toEndOf, Right_toRightOf, Baseline_toBaselineOf, Vertical_bias, Horizontal_bias |
TableLayout | TableLayout, TableRow, background, padding |
ScrollView | ScrollView, android.support.v4.widget.NestedScrollView, HorizontalScrollView, android:scrollbars |
LinearLayout的螢幕截圖:

image

image
RelativeLayout的螢幕截圖:

image

image
FrameLayout的螢幕截圖:

image

image
ConstraintLayout的螢幕截圖(這個也做了移植,確實經驚艷到我了):

image

image
TableLayout的螢幕截圖:

image

image
ScrollView的螢幕截圖:

ScrollView_ScreenRec.gif
資產目錄文件的轉換
這一部分內容比較多,簡單的說就是把Android工程的res目錄的文件轉換成iOS工程的Assets.xcassets目錄的文件。
- mipmap目錄的圖片資源:Android工程的mdpi,hdpi,xhdpi和xxhdpi的影像資源被映射到iOS工程的1x,2x和3x影像資源路徑裡面。
- 針對
.9.png圖片文件:在catalog中為asset添加了切片(slicing )資訊。 - Vector圖片形式的xml被轉換成Storyboard上能夠使用的
.pdf文件。 - 向量影像xml也被轉換成Swift程式碼,並在
VectorStore.swift中為每個vector文件添加一個靜態方法。
除此之外還支援.png,color形式的xml,Shape形式的xml,Animation形式的xml的轉換。
以下是使用截圖:
Android和iOS設備的顯示了各種資產的使用情況的螢幕截圖:

image
Android Studio布局編輯器:

image
Xcode Storyboard編輯器:

image
Android Studio Asset資源圖片:

image
Xcode Asset 面板:

image
Android Studio .9.PNG 圖片資源編輯器:

image
Xcode Asset面帶有從.9.png文件中提取的切片資訊:

image
Vector矢量圖的轉換截圖對比:

image
Shape xml文件的對比:

image
動畫轉換的對比:
Android | iOS |
|---|---|
事實上效果是同步的,只是錄屏和操作步驟不一致的原因,看起來有些延遲。
外部庫Glide的轉換
支援的功能:
- 從本地res文件夾載入drawable
- 從URL載入圖片
- 將圖片載入到UIImageView
- 在下載過程中提供佔位符圖片
- 在圖片之間應用過渡:CrossFade,Flip
- 支援圖片Transformation (變換)API:circleCrop(),centerCrop(),fitCenter(),centerInside()
- 清除待處理的請求
- RequestOptions類可在實例之間重用設置
待支援的API:
- priority()
- diskCacheStrategy()
- asBitmap()
- error()
- fallback()
- clearMemory()
使用截圖:

image
計算器App
這是一個移植的計算器項目。
使用截圖如下,還是挺酷選的呢,效果保持一致,操作也很流暢:

image
最後附上官網地址,有興趣的可以去看看。
官網地址:http://www.reflectcode.com/
Github: https://github.com/ReflectCode/
