How by Lazy Works

Implement a simpler version of by lazy

Guowei

2 minute read

by lazy is implemented by using the “property delegation” in Kotlin. But if you look into the source code and trying to understand what is going on, it can be confusing, because it is full of locks and generics and where’s the `getValue()`` function they say that the delegation must implement?? In the handmade spirit (best way to learn is by doing it yourself), let’s do a stripped down version ourselves.

Kotlin Noinline and Crossinline

What is noinline and crossinline?

Guowei Lv

4 minute read

Compile time constant const val NAME = "Guowei" fun main() { tv.text = NAME } After compile it will (almost) look like this: // Imaginary code fun main() { tv.text = "Guowei" } inline function We can do similar things to functions, by adding keyword inline. inline fun hello() { println("hello") } fun main() { hello() } So at compile time, the hello() function will be copied to the calling place:

View Binding Internals

Peek into the internals of view binding

Guowei Lv

1 minute read

I was bored at night so decided to peek into the guts of how Android’s View Binding works. To my suprise the generated code is extremely simple. Imagine you have a list_item.xml file and it looks like this: <LinearLayout> <ImageView android:id="@+id/icon" /> <TextView android:id="@+id/name" /> </LinearLayout> Then the generated class will be like this: public final class ListItemBinding implements ViewBinding { private final LinearLayout rootView; public final ImageView icon; public final TextView name; // Notice: private constructor private ListItemBinding(LinearLayout rootView, ImageView icon, TextView name) { this.

Next Job Oriented Programming

most popular programming paradigm?

Guowei Lv

1 minute read

Rant alert I’ve been having this idea for a while, and when having dinner with my other programmer friend yesterday, I jokingly said that “Have you noticed there is a very popular programming paradigm that’s been adopted everywhere but no one is aware of it?” Yes, it is what I call Next Job Oriented Programming(NJOP). Don’t get me wrong, I’m not against adopting new tech by any means, it’s good and healthy to keep our tech stack up to date.

Android Custom View 102 (Part 19)

More Advanced ObjectAnimator examples

Guowei Lv

2 minute read

In this post let’s take a deeper look at some of the more advanced uses of ObjectAnimator. KeyFrame First example is the usage of KeyFrames. Here is the final result:

Let’s look at the code val imageView = findViewById<ImageView>(R.id.imageView) /** In total we want to move 300dp. 0% of time passed, it has moved 0dp. 30% of time passed, it has moved 100dp. 60% of time passed, it has moved 120dp.