Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.80 KB | None | 0 0
  1. package com.astar.saberapp.ui.views
  2.  
  3. import android.content.Context
  4. import android.graphics.Canvas
  5. import android.graphics.Color
  6. import android.graphics.Paint
  7. import android.graphics.RectF
  8. import android.util.AttributeSet
  9. import android.util.Log
  10. import android.view.View
  11. import com.astar.saberapp.R
  12. import com.astar.saberapp.util.dpToPx
  13.  
  14. class PaletteView @JvmOverloads constructor(
  15.     context: Context,
  16.     attrs: AttributeSet? = null,
  17.     defStyleAttr: Int = 0
  18. ) : View(context, attrs, defStyleAttr) {
  19.  
  20.     companion object {
  21.         private const val TAG = "Palette View"
  22.  
  23.         private const val DEFAULT_SIZE = 40                     // минимальный размер по умолчанию
  24.         private const val DEFAULT_BACKGROUND = Color.WHITE // фон по умолчанию
  25.         private const val DEFAULT_COUNT_COLORS = 2              // количество секций по умолчанию
  26.     }
  27.  
  28.     private var maxCountColors = 0
  29.     private var colorsList = arrayListOf<Int>()
  30.     private var background = DEFAULT_BACKGROUND
  31.  
  32.     private var countColors = 0                     // количество цветов
  33.     private var widthRect: Float = width.toFloat()  // ширина одно элемента, по умолчанию на весь экран
  34.     private var rect = RectF(0f, 0f, widthRect, height.toFloat())
  35.  
  36.     private lateinit var paint: Paint
  37.  
  38.     init {
  39.         if (attrs != null) {
  40.             val typedArray = context.theme.obtainStyledAttributes(
  41.                 attrs,
  42.                 R.styleable.PaletteView,
  43.                 0,
  44.                 0
  45.             )
  46.             maxCountColors = typedArray.getInt(
  47.                 R.styleable.PaletteView_pv_maxCountItems,
  48.                 DEFAULT_COUNT_COLORS
  49.             )
  50.             background = typedArray.getColor(
  51.                 R.styleable.PaletteView_pv_background,
  52.                 DEFAULT_BACKGROUND
  53.             )
  54.             typedArray.recycle()
  55.         }
  56.     }
  57.  
  58.     private val paintList = arrayListOf<Paint>()
  59.  
  60.     fun setColors(vararg colors: Int) {
  61.         colorsList.clear()
  62.         for (color in colors) {
  63.             colorsList.add(color)
  64.             val paint = Paint(Paint.ANTI_ALIAS_FLAG)
  65.             paint.color = color
  66.             paintList.add(paint)
  67.         }
  68.  
  69.         countColors = colorsList.size
  70.         invalidate()
  71.     }
  72.  
  73.     override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
  74.         super.onSizeChanged(w, h, oldw, oldh)
  75.         widthRect = (w / countColors).toFloat()
  76.         rect.left = 0f
  77.         rect.top = 0f
  78.         rect.right = widthRect
  79.         rect.bottom = h.toFloat()
  80.     }
  81.  
  82.     override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
  83.         super.onMeasure(widthMeasureSpec, heightMeasureSpec)
  84.  
  85.         val initSizeWidth = resolveDefaultSize(widthMeasureSpec)
  86.         val initSizeHeight = resolveDefaultSize(heightMeasureSpec)
  87.         setMeasuredDimension(initSizeWidth, initSizeHeight)
  88.         Log.e(TAG, "onMeasure after set size: $measuredWidth $measuredHeight")
  89.     }
  90.  
  91.     private fun resolveDefaultSize(spec: Int): Int {
  92.         return when (MeasureSpec.getMode(spec)) {
  93.             MeasureSpec.UNSPECIFIED -> context.dpToPx(DEFAULT_SIZE).toInt()
  94.             MeasureSpec.AT_MOST -> MeasureSpec.getSize(spec)
  95.             MeasureSpec.EXACTLY -> MeasureSpec.getSize(spec)
  96.             else -> MeasureSpec.getSize(spec)
  97.         }
  98.     }
  99.  
  100.     override fun onDraw(canvas: Canvas) {
  101.         super.onDraw(canvas)
  102.        
  103.         for (i in 0 until countColors) {
  104.             println(widthRect)
  105.             canvas.drawRect(rect, paintList[i])
  106.             rect.right += widthRect + widthRect
  107.             rect.left += widthRect
  108.         }
  109.  
  110.         Log.d(TAG, "onDraw: Перерисовываем...")
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement