Anko
Anko 是一个 DSL (Domain-Specific Language), 它是JetBrains出品的,用 Kotlin 开发的安卓框架。它主要的目的是用来替代以前XML的方式来使用代码生成UI布局。
先来看一个直观的例子
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <EditText android:id="@+id/todo_title" android:layout_width="match_parent" android:layout_heigh="wrap_content" android:hint="@string/title_hint" /> <!-- Cannot directly add an inline click listener as onClick delegates implementation to the activity --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/add_todo" /> </LinearLayout>
使用Anko之后,可以用代码实现布局,并且button还绑定了点击事件。
verticalLayout { var title = editText { id = R.id.todo_title hintResource = R.string.title_hint } button { textResource = R.string.add_todo onClick { view -> { // do something here title.text = "Foo" } } } }
可以看到 DSL 的一个主要优点在于,它需要很少的时间即可理解和传达某个领域的详细信息。
简单封装OkHttp
OkHttp是一个成熟且强大的网络库,在Android源码中已经使用OkHttp替代原先的HttpURLConnection。很多著名的框架例如Picasso、Retrofit也使用OkHttp作为底层框架。在这里我对OkHttp做一下简单的封装,其实封装得有点粗暴只是为了演示如何实现dsl。
import io.reactivex.BackpressureStrategy import io.reactivex.Flowable import io.reactivex.schedulers.Schedulers import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody import okhttp3.Response import java.util.concurrent.TimeUnit /** * Created by Tony Shen on 2017/6/1. */ class RequestWrapper { var url:String? = null var method:String? = null var body: RequestBody? = null var timeout:Long = 10 internal var _success: (String) -> Unit = { } internal var _fail: (Throwable) -> Unit = {} fun onSuccess(onSuccess: (String) -> Unit) { _success = onSuccess } fun onFail(onError: (Throwable) -> Unit) { _fail = onError } } fun http(init: RequestWrapper.() -> Unit) { val wrap = RequestWrapper() wrap.init() executeForResult(wrap) } private fun executeForResult(wrap:RequestWrapper) { Flowable.create<Response>({ e -> e.onNext(onExecute(wrap)) }, BackpressureStrategy.BUFFER) .subscribeOn(Schedulers.io()) .subscribe( { resp -> wrap._success(resp.body()!!.string()) }, { e -> wrap._fail(e) }) } private fun onExecute(wrap:RequestWrapper): Response? { var req:Request? = null when(wrap.method) { "get","Get","GET" -> req =Request.Builder().url(wrap.url).build() "post","Post","POST" -> req = Request.Builder().url(wrap.url).post(wrap.body).build() "put","Put","PUT" -> req = Request.Builder().url(wrap.url).put(wrap.body).build() "delete","Delete","DELETE" -> req = Request.Builder().url(wrap.url).delete(wrap.body).build() } val http = OkHttpClient.Builder().connectTimeout(wrap.timeout, TimeUnit.SECONDS).build() val resp = http.newCall(req).execute() return resp }
封装完OkHttp之后,看看如何来编写get请求
http { url = "http://www.163.com/" method = "get" onSuccess { string -> L.i(string) } onFail { e -> L.i(e.message) } }
是不是很像以前用jquery来写ajax?
post请求也是类似的,只不过多了body
var json = JSONObject() json.put("xxx","yyyy") .... val postBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json.toString()) http { url = "https://......" method = "post" body = postBody onSuccess { string -> L.json(string) } onFail { e -> L.i(e.message) } }
封装自己的图像处理框架
cv4j 是我们开发的实时图像处理框架。最早使用滤镜的方式如下:
CV4JImage cv4jImage = new CV4JImage(bitmap); CommonFilter filter = new NatureFilter(); Bitmap newBitMap = filter.filter(cv4jImage.getProcessor()).getImage().toBitmap(); image.setImageBitmap(newBitMap);
后来增加了RxJava封装的版本
RxImageData.bitmap(bitmap).addFilter(new NatureFilter()).into(image);
现在Kotlin项目除了可以使用上述两种方式之外,还多了一种方式。
cv4j { bitmap = BitmapFactory.decodeResource(resources, R.drawable.test_io) filter = NatureFilter() imageView = image }
这个dsl是如何封装的呢?其实是对RxJava版本进一步封装。
/** * Copyright (c) 2017-present, CV4J Contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.cv4j.rxjava import android.app.Dialog import android.graphics.Bitmap import android.widget.ImageView import com.cv4j.core.datamodel.CV4JImage import com.cv4j.core.filters.CommonFilter /** * only for Kotlin code,this class provides the DSL style for cv4j */ class Wrapper { var bitmap:Bitmap? = null var cv4jImage: CV4JImage? = null var bytes:ByteArray? = null var useCache:Boolean = true var imageView: ImageView? = null var filter: CommonFilter? = null var dialog: Dialog? = null } fun cv4j(init: Wrapper.() -> Unit) { val wrap = Wrapper() wrap.init() render(wrap) } private fun render(wrap: Wrapper) { if (wrap.bitmap!=null) { if (wrap.filter!=null) { RxImageData.bitmap(wrap.bitmap).dialog(wrap.dialog).addFilter(wrap.filter).isUseCache(wrap.useCache).into(wrap.imageView) } else { RxImageData.bitmap(wrap.bitmap).dialog(wrap.dialog).isUseCache(wrap.useCache).into(wrap.imageView) } } else if (wrap.cv4jImage!=null) { if (wrap.filter!=null) { RxImageData.image(wrap.cv4jImage).dialog(wrap.dialog).addFilter(wrap.filter).isUseCache(wrap.useCache).into(wrap.imageView) } else { RxImageData.image(wrap.cv4jImage).dialog(wrap.dialog).isUseCache(wrap.useCache).into(wrap.imageView) } } else if (wrap.bytes!=null) { if (wrap.filter!=null) { RxImageData.bytes(wrap.bytes).dialog(wrap.dialog).addFilter(wrap.filter).isUseCache(wrap.useCache).into(wrap.imageView) } else { RxImageData.bytes(wrap.bytes).dialog(wrap.dialog).isUseCache(wrap.useCache).into(wrap.imageView) } } }
来看一下程序的最终效果图
dsl风格使用滤镜.png
cv4j 目前已经支持了几十种滤镜,当然除了滤镜还有其他功能,感兴趣的童鞋可以看我们的源码:)。
总结
使用dsl的代码风格,可以让程序更加直观和简洁。如果使用Kotlin来开发项目的话,完全可以尝试一下。
公司的sdk项目我也考虑引入Kotlin,我已经写了一个module用于封装原先的sdk,这个module只适用于Kotlin项目。用于简化初始化sdk和实现deep link的注册服务。
初始化sdk.jpg
注册各个mLink服务.jpg
可以感受一下,使用dsl是不是比原先的代码更加简洁和直观呢?
另外,众所周知的Gradle也是基于DSL的Java构建工具。