scaloid開發android

本篇文章被從新寫了一遍,基本上一篇文章寫完後,過20天后,就發現之前的觀點站不住腳了。
java

scaloid強大的表現力android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content" android:padding="20dip">
    <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content" android:text="Sign in"
            android:layout_marginBottom="25dip" android:textSize="24.5sp"/>
    <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content" android:text="ID"/>
    <EditText android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@+id/userId"/>
    <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content" android:text="Password"/>
    <EditText android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@+id/password"
            android:inputType="textPassword"/>
    <Button android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@+id/signin"
            android:text="Sign in"/>
    <LinearLayout android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Button android:text="Help" android:id="@+id/help"
                android:layout_width="match_parent" android:layout_height="wrap_content"/>
        <Button android:text="Sign up" android:id="@+id/signup"
                android:layout_width="match_parent" android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

is reduced togit

new SVerticalLayout {
  STextView("Sign in").textSize(24.5 sp).<<.marginBottom(25 dip).>>
  STextView("ID")
  SEditText()
  STextView("Password")
  SEditText() inputType TEXT_PASSWORD
  SButton("Sign in")
  this += new SLinearLayout {
    SButton("Help")
    SButton("Sign up")
  }
}.padding(20 dip)

以上摘自scaloid github主頁,是否是被其簡潔性打動了呢,無論你有沒有,至少我是心動的不行了。因而乎,這幾天將 《瘋狂android講義》前面關於UI組件講解的幾個重點例子,用scaloid翻譯了一遍。而後,現實告訴咱們,簡潔是有代價的...程序員

activity初始化模版

class clazzName extends SActivity{activity=>
    lazy val comp1= new SUIComponent().styles//...
    lazy val comp2= new SUIComponent().styles//...
    onCreate{
        contentView = new SUILayout{
            this+=com1.<<.layoutStyles.>>
            this+=com1.<<.layoutStyles.>>
        }
        comp1.onClick{(_:android.widget.AdapterView[_], _:android.view.View,position: Int, _:Long)=>
            //todo
        }
    }
}

這基本就是我關於UI初始化的標準模版了(你也能夠將lazy val換成var )。至於爲何不將layoutStyles寫在聲明後面,則是因爲scaloid關於UI組件的實現都是繼承與原生android api,並使用了的隱式轉換(implicit context:Context) 來簡化代碼量。而渲染效率呢,則和用xml寫佈局的沒什麼區別。
這時,便出現了第一個問題:到底使用xml寫view仍是說用代碼寫view?github

雖然最近一直在用代碼寫view,熟悉id,在實際生產環境下,我更傾向於使用後者,緣由以下:api

  • java和scala之間切換更容易網絡

  • scala編譯速度真的很慢函數

  • 能夠直接經過可視化工具生成xml(據我瞭解,有很多android程序員都是經過拖拽來實現佈局的)工具

  • 減小progurd配置佈局

寫view組件

當你要開發view組件時,最好參照scaloid已經寫好的組件。例如你要實現一個View,最好時繼承scaloid的 TraitView,這樣你才能擁有一些簡便的api。還有一些構造函數等問題,基本上照着scaloid源碼來,基本上沒啥大問題。

scaloid資料

scaloid的網絡資源真心不豐富,github上的readme+官方blog幾乎就是全部資料了。不少api都沒有覆蓋到,基本上你要用到哪裏,就要去扒一下源碼才能夠。我在裏碰到的問題呢,是說,一些java原生api(例如 SimpleArrayAdapter)是不支持scala集合的?

這裏的解決方案:

  1. 集合等api所有使用java原生的

  2. 使用scala集合,而後進行類型轉換

  3. 摒棄掉java原生api,使用scaloid提供的或者本身封裝

以上方案,用第一種,就真心不必使用scala語言了,直接java寫會更省勁;第二種,最好本身封裝一些隱式轉換加快開發;第三種,初期比較痛苦,得查scaloid api以及看源碼,才能掌握好用法,日後發展的話,仍是蠻ok的。


scaloid和proguard

貌似只要和scala占上邊的就沒有什麼是簡單的,不論是scala的類型系統仍是sbt build工具,每個都須要花大量的時間來入門。當scala與android結合時,更是如此。

當你用scala開發android正爽的時候,忽然發現編譯報 classNotFound 的錯誤,而產生這個問題根源則是proguard剪切代碼帶來的,若是你懶的解決這個問題呢,能夠直接sbt 運行 clean  android:run 指令。若是要想好好的解決這個問題呢,推薦看看douban-android項目的以proguard開頭的兩個配置文件。


scaloid對原有api的簡化

除了view組件的api進行簡化外,還對log,ui更新等作了簡化,簡化後,用起來真心溫馨,github上關於簡化的講解,基本能讓你上手使用。代碼量有大幅的減小。

遇到的小坑

與其說是小坑,倒不如說時對api的掌握程度不夠形成的。將小坑記錄在這裏

//顏色轉換
new ColorDrawable(Color.parseColor("#F00000"))

//動畫效果  
AnimationUtils.loadAnimation(context,android.R.anim.fade_out)

還有一些是java和scala混用帶來的一些問題,這裏就不作贅述了,若是真想用scala作開發的話,就儘可能減小直接調用原生java api的機會。還有就是scala開發和java開發仍是有不少細節上的不一樣,一些思惟上仍是要作一些轉變的,如今用scaloid寫的開源的android真心少,開發技巧更多仍是從一些後臺代碼中學習吧。

小結

總的來說,除了一向的編譯速度慢之外,用scala開發android,對源碼的閱讀和學習能力要求仍是蠻高的,若是沒有真正的需求或者能跟得上技術的團隊的話,仍是用java開發好一點,簡單即效率。

相關文章
相關標籤/搜索