attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : View(context, attrs, defStyleAttr, defStyleRes) {
private val borderImage: Drawable?
private val foregroundImage: Drawable?
private val gradient: Drawable?
private val scaleXForForegroundImage: Float
private var gestureDetector: GestureDetector? = null
init {
val typedArray = context.obtainStyledAttributes(
attrs,
R.styleable.CustomButton,
defStyleAttr,
defStyleRes
)
borderImage = typedArray.getDrawable(R.styleable.CustomButton_borderImage)
foregroundImage = typedArray.getDrawable(R.styleable.CustomButton_foregroundImage)
scaleXForForegroundImage = typedArray.getFloat(R.styleable.CustomButton_scaleXForForegroundImage, 1f)
gradient = typedArray.getDrawable(R.styleable.CustomButton_gradient)
gestureDetector = GestureDetector(context, SingleTapConfirm())
typedArray.recycle()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
drawForegroundImage(canvas)
drawGradient(canvas)
drawText(canvas)
drawBorderImage(canvas)
}
private fun drawBorderImage(canvas: Canvas) {
borderImage?.setBounds(0, 0, width, height)
borderImage?.draw(canvas)
}
private fun drawForegroundImage(canvas: Canvas) {
foregroundImage?.setBounds(0, 0, width, (scaleXForForegroundImage * height).toInt())
foregroundImage?.draw(canvas)
}
private fun drawGradient(canvas: Canvas) {
gradient?.setBounds(0, 0, width, height)
gradient?.draw(canvas)
}
private fun drawText(canvas: Canvas) {
}
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
//setBackgroundColor(Color.BLUE)
return true
}
MotionEvent.ACTION_UP -> {
//setBackgroundColor(Color.RED)
//
return false
}
}
invalidate()
return true
}
Что не работает?
Обсуждают сегодня