preface

It has been a long time since the last article, during which many things have happened, such as Kotlin’s popularity, such as the author I have to go back home to get the certificate.

About Kotlin’s popularity, I always thought it was a great language, even if it wasn’t popular. But now that Google has paved the way for it, the future looks pretty good.

Given Kotlin’s popularity, similar articles are springing up all over the place. The author’s original title, “What’s It Like to Develop Android with Kotlin?” It doesn’t seem to work that well. After all, many Android developers are already experimenting, some may be dabbling in it, some may be using it in depth, but I personally don’t think it makes sense to write some basic grammar articles at this time.

I’ve been working on an interesting open source project with Kotlin recently, which is a bit incomplete, but still barely complete. This article should serve as an introduction to my little project, and give those of you who are new to Kotlin an idea of what you can do with a language like this.

DSL

Many friends have done custom view, also know that there are two kinds of custom view in Android:

1. Inherit from ViewGroup and combine multiple views to form 2. Inherited from View, on canvas (canvas) through the corresponding API drawn

I have suffered a lot from CustomView Hell because of my employer’s bizarre requirements, and I understand that there are occasions for both solutions. Scheme 1 is relatively easy to get used to, with basic knowledge of layout and writing, while Scheme 2 requires a certain learning cost and experience to deal with various problems encountered. Finally, it is complicated to write. Is there any way to make canvas drawing easier?

After working with Anko, I realized how elegant and simple a code layout can be. This is not an advertisement for Anko, but for anko layout:

verticalLayout {
    textView {
        text = "Write anything."
        textSize = 20f
    }

    imageView {
        imageResource = R.mipmap.ic_launcher
    }
}
Copy the code

Code like this should all look familiar:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}}Copy the code

Yes, anko’s layout is similar to Gradle’s syntax, except for a few differences, you might even think it’s the same language. That’s what I’m going to introduce you to, domain-Specific Languages.

MagicPen

MagicPen is an open source project I started with Kotlin for manipulating Canvas custom Views. Is that a little vague? So let’s go straight to the code and the diagram

package com.lab.zhangll.magicpen

import android.graphics.Color
import android.graphics.Paint
import android.graphics.PointF
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.lab.zhangll.magicpen.lib.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(magicPen {val line = line {start = PointF(0f, 1000f)) } val bigOne = circle {radius = 200f // aboveOf(line) // leftMargin = 500f // leftMargin Paint = paint (). Apply {color = color.red} // RED} text {content = paint ()"I am a little bird"Paint = paint ().apply {textSize = 40f} centerIn(bigOne)} circle {radius = 50f // Radius centerIn(bigOne) // in the middle of the big circle gesture { onClick = { Toast.makeText(this@MainActivity,"clicked", toast.length_short).show()} // when clicking onDragBy = {x, y -> moveBy(x, Y)} onRelease = {smoothMoveToOrigin()}Copy the code

With package, import, and my conscious blank lines, that’s 50 lines. Let’s see what it does

I believe that the text and text together, coupled with the code in the comments, we can easily see understand. I made this view just like the layout. Including line, circle, text three kinds of graphics; Also includes absolute position, relative relation; There are even click events, drag events, and smooth motion animations. Pretty lean, right?

Unfortunately, that’s about it for now, as the project has only just been launched, and two potential partners will have to learn Kotlin and work on the project for a while. But in the near future, MagicPen will be able to create as many custom Views as I can think of, so stay tuned.

Project source code on the https://github.com/neverwoodsS/MagicPen welcome onlookers and put forward

PS. This is my last article as a single youth