Created
November 22, 2018 20:12
-
-
Save bitvale/e12e9703ab09615a50e41c8a7ae2a110 to your computer and use it in GitHub Desktop.
For Medium article "Android Dynamic Custom View is Easy"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Switcher @JvmOverloads constructor( | |
| context: Context, | |
| attrs: AttributeSet ? = null, | |
| defStyleAttr: Int = 0 | |
| ) : View(context, attrs, defStyleAttr) { | |
| init { | |
| attrs?.let { retrieveAttributes(attrs, defStyleAttr) } | |
| } | |
| private fun retrieveAttributes(attrs: AttributeSet, defStyleAttr: Int) { | |
| // retrieve cutom attributes | |
| } | |
| override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { | |
| // setup switcher width and height | |
| } | |
| override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { | |
| // setup helper sizes every time switcher size changed (radius, icon width...) | |
| } | |
| override fun onDraw(canvas: Canvas ?) { | |
| // just draw | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
attrs?.let { retrieveAttributes(attrs, defStyleAttr) }should beattrs?.let { retrieveAttributes(it, defStyleAttr) }