|
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) |
|
} |
|
|
|
} |