Skip to content

Instantly share code, notes, and snippets.

@GerganaT
Created September 9, 2021 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GerganaT/0625a8a1139a005c48e9a4a04eb09322 to your computer and use it in GitHub Desktop.
Save GerganaT/0625a8a1139a005c48e9a4a04eb09322 to your computer and use it in GitHub Desktop.
loading button code in progress
package com.udacity
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import kotlin.properties.Delegates
class LoadingButton @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private var buttonBackgroundColor = 0
private var buttonTextColor = 0
private var widthSize = resources.getDimension(R.dimen.buttonWidth)
private var heightSize = resources.getDimension(R.dimen.buttonHeight)
private val valueAnimator = ValueAnimator()
private val rect = RectF(0f, 0f, widthSize, heightSize)
private var buttonState: ButtonState by Delegates.observable(ButtonState.Completed)
{ p, old, new ->
}
private val buttonBackgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
}
private val buttonTextPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
textSize = resources.getDimension(R.dimen.default_text_size)
textAlign = Paint.Align.CENTER
}
init {
buttonBackgroundColor = resources.getColor(R.color.colorPrimary, null)
buttonTextColor = resources.getColor(R.color.white, null)
isClickable = true
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas?.apply {
buttonBackgroundPaint.color = buttonBackgroundColor
buttonTextPaint.color = buttonTextColor
drawRect(rect, buttonBackgroundPaint)
drawText(
resources.getString(
R.string.download_button_label
),
widthSize / 2, heightSize / 2, buttonTextPaint
)
}
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val minw: Int = paddingLeft + paddingRight + suggestedMinimumWidth
val w: Int = resolveSizeAndState(minw, widthMeasureSpec, 1)
val h: Int = resolveSizeAndState(
MeasureSpec.getSize(w),
heightMeasureSpec,
0
)
this.widthSize = w.toFloat()
heightSize = h.toFloat()
setMeasuredDimension(w, h)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment