Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.astar.saberapp.ui.views
- import android.content.Context
- import android.graphics.Canvas
- import android.graphics.Color
- import android.graphics.Paint
- import android.graphics.RectF
- import android.util.AttributeSet
- import android.util.Log
- import android.view.View
- import com.astar.saberapp.R
- import com.astar.saberapp.util.dpToPx
- class PaletteView @JvmOverloads constructor(
- context: Context,
- attrs: AttributeSet? = null,
- defStyleAttr: Int = 0
- ) : View(context, attrs, defStyleAttr) {
- companion object {
- private const val TAG = "Palette View"
- private const val DEFAULT_SIZE = 40 // минимальный размер по умолчанию
- private const val DEFAULT_BACKGROUND = Color.WHITE // фон по умолчанию
- private const val DEFAULT_COUNT_COLORS = 2 // количество секций по умолчанию
- }
- private var maxCountColors = 0
- private var colorsList = arrayListOf<Int>()
- private var background = DEFAULT_BACKGROUND
- private var countColors = 0 // количество цветов
- private var widthRect: Float = width.toFloat() // ширина одно элемента, по умолчанию на весь экран
- private var rect = RectF(0f, 0f, widthRect, height.toFloat())
- private lateinit var paint: Paint
- init {
- if (attrs != null) {
- val typedArray = context.theme.obtainStyledAttributes(
- attrs,
- R.styleable.PaletteView,
- 0,
- 0
- )
- maxCountColors = typedArray.getInt(
- R.styleable.PaletteView_pv_maxCountItems,
- DEFAULT_COUNT_COLORS
- )
- background = typedArray.getColor(
- R.styleable.PaletteView_pv_background,
- DEFAULT_BACKGROUND
- )
- typedArray.recycle()
- }
- }
- private val paintList = arrayListOf<Paint>()
- fun setColors(vararg colors: Int) {
- colorsList.clear()
- for (color in colors) {
- colorsList.add(color)
- val paint = Paint(Paint.ANTI_ALIAS_FLAG)
- paint.color = color
- paintList.add(paint)
- }
- countColors = colorsList.size
- invalidate()
- }
- override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
- super.onSizeChanged(w, h, oldw, oldh)
- widthRect = (w / countColors).toFloat()
- rect.left = 0f
- rect.top = 0f
- rect.right = widthRect
- rect.bottom = h.toFloat()
- }
- override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec)
- val initSizeWidth = resolveDefaultSize(widthMeasureSpec)
- val initSizeHeight = resolveDefaultSize(heightMeasureSpec)
- setMeasuredDimension(initSizeWidth, initSizeHeight)
- Log.e(TAG, "onMeasure after set size: $measuredWidth $measuredHeight")
- }
- private fun resolveDefaultSize(spec: Int): Int {
- return when (MeasureSpec.getMode(spec)) {
- MeasureSpec.UNSPECIFIED -> context.dpToPx(DEFAULT_SIZE).toInt()
- MeasureSpec.AT_MOST -> MeasureSpec.getSize(spec)
- MeasureSpec.EXACTLY -> MeasureSpec.getSize(spec)
- else -> MeasureSpec.getSize(spec)
- }
- }
- override fun onDraw(canvas: Canvas) {
- super.onDraw(canvas)
- for (i in 0 until countColors) {
- println(widthRect)
- canvas.drawRect(rect, paintList[i])
- rect.right += widthRect + widthRect
- rect.left += widthRect
- }
- Log.d(TAG, "onDraw: Перерисовываем...")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement