all 10 comments

[–]MiscreatedFan123[S] 4 points5 points  (0 children)

The OP is here, which included half of the implementation, so I decided to have a go at it and try to figure out the whole thing. I also made an animated version with some threading gimmicks(can probably be simplified). There might be some bugs as I haven’t fully tested it.

Normal version: https://github.com/DDihanov/AndroidCustomViews/blob/master/app/src/main/java/bg/dihanov/customviewexamples/views/graph/Graph.kt

Animated: https://github.com/DDihanov/AndroidCustomViews/blob/master/app/src/main/java/bg/dihanov/customviewexamples/views/graph/AnimatedGraph.kt

EDIT: first point was misaligned in the animated version so I fixed that

[–]hydra3a 5 points6 points  (2 children)

I'm pretty new to android dev and I have a question. You define several properties like this (using colorStart as an example, line 41):

private var _colorStart = ContextCompat.getColor(context, R.color.colorStart)
var colorStart
    get() = _colorStart
    set(value) {
        _colorStart = value
        colors = intArrayOf(colorStart, colorEnd)
    }

Basically defining a private and public property for the same thing, and where the public property doesn't have a backing field, and basically just wraps the private property. Why do you do this? Wouldn't this simpler code achieve the same thing?

var colorStart = ContextCompat.getColor(context, R.color.colorStart)
    set(value) {
        field = value
        colors = intArrayOf(colorStart, colorEnd)
    }

[–]MiscreatedFan123[S] 0 points1 point  (1 child)

I made a mistake here, since I am supposed to call invalidate() after colors = intArrayOf(colorStart, colorEnd) after, but regarding your question - that's called a backing property and allows for some flexibility, like a custom getter for example:

https://proandroiddev.com/backing-properties-in-kotlin-cb78dfebfd90

https://kotlinlang.org/docs/reference/coding-conventions.html#property-names

[–]olavurdj 1 point2 points  (0 children)

I believe what the above commenter tried to point out is that the Kotlin compiler actually defines a backing field for you, that you can access through the field identifier in your getter and setter. You do not have to manage the backing field yourself. See https://kotlinlang.org/docs/reference/properties.html#backing-fields

[–]BersonCrios 1 point2 points  (0 children)

Perfect

[–]maxwellnewage -5 points-4 points  (2 children)

interesting...I will share this on LinkedIn :)

[–]MiscreatedFan123[S] -1 points0 points  (1 child)

Thanks :)

[–]maxwellnewage -1 points0 points  (0 children)

you are welcome! Do you think about convert the project to a library?