RecyclerViewで 折れ線グラフを作る

B8f695167d115eef9f32399b1c9261df?s=47 Yuki Mima
December 12, 2019

RecyclerViewで 折れ線グラフを作る

B8f695167d115eef9f32399b1c9261df?s=128

Yuki Mima

December 12, 2019
Tweet

Transcript

  1. 5.
  2. 12.
  3. 14.
  4. 15.
  5. 17.
  6. 18.

     

  7. 19.

     

  8. 21.
  9. 23.

    abstract class HorizontalDateItemDecoration( private val _context: Context ) : RecyclerView.ItemDecoration(),

    ViewExtensions { private val height = 64.dp private val textSize = 14f.sp private val backgroundPaint = Paint().apply { color = Color.parseColor("#000000") } private val dayTextPaint = Paint().apply { color = Color.parseColor("#FF00FF") textSize = this@HorizontalDateItemDecoration.textSize isAntiAlias = true } private val yearAndMonthPaint = Paint().apply { color = Color.parseColor("#FFFF00") textSize = this@HorizontalDateItemDecoration.textSize isAntiAlias = true } override fun getContext(): Context = _context override fun onDraw( canvas: Canvas, parent: RecyclerView, state: RecyclerView.State ) { canvas.drawRect(0f, 0f, canvas.width.toFloat(), height.toFloat(), backgroundPaint) var isVisibleFirstDay = false var isVisibleLastDay = false var lastVisibleYearAndMonthText = "" var lastVisibleYearAndMonthY = 0f parent.children.forEach { view -> val adapterPosition = parent.getChildAdapterPosition(view) val localDateFromPosition = getDate(adapterPosition) //೔෇ͷඳը val dayText = localDateFromPosition.dayOfMonth.toString() val dayTextSizeRect = getTextSizeRect(dayTextPaint, dayText) val dayTextX = view.x + (view.width - dayTextSizeRect.width()) / 2 val dayTextY = dayTextSizeRect.height().toFloat() + (height / 2 - dayTextSizeRect.height()) / 2 + height / 2 canvas.drawText(dayText, dayTextX, dayTextY, dayTextPaint) //೥݄ͷඳը val yearAndMonthText = "${localDateFromPosition.year}೥${localDateFromPosition.monthValue}݄" val yearAndMonthTextSizeRect = getTextSizeRect(yearAndMonthPaint, yearAndMonthText) val yearAndMonthY = yearAndMonthTextSizeRect.height().toFloat() + (height / 2 - yearAndMonthTextSizeRect.height()) / 2 val isFirstDayOfMonth = localDateFromPosition.isEqual(localDateFromPosition.with(firstDayOfMonth())) val isLastDayOfMonth = localDateFromPosition.isEqual(localDateFromPosition.with(lastDayOfMonth())) when { isFirstDayOfMonth -> { (view.x).let { x -> if (x > 0) { canvas.drawText(yearAndMonthText, x, yearAndMonthY, yearAndMonthPaint) } else { canvas.drawText(yearAndMonthText, 0f, yearAndMonthY, yearAndMonthPaint) } } isVisibleFirstDay = true } isLastDayOfMonth -> { (view.x + view.width - yearAndMonthTextSizeRect.width()).let { x -> if (x < 0) { canvas.drawText(yearAndMonthText, x, yearAndMonthY, yearAndMonthPaint) } else { canvas.drawText(yearAndMonthText, 0f, yearAndMonthY, yearAndMonthPaint) } } isVisibleLastDay = true } } lastVisibleYearAndMonthText = yearAndMonthText lastVisibleYearAndMonthY = yearAndMonthY } //೥݄ͷඳը if (!isVisibleFirstDay && !isVisibleLastDay) { canvas.drawText( lastVisibleYearAndMonthText, 0f, lastVisibleYearAndMonthY, yearAndMonthPaint ) } } override fun getItemOffsets( outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State ) { outRect.set(0, height, 0, 0) } abstract fun getDate(position: Int): LocalDate }
  10. 24.
  11. 25.
  12. 27.
  13. 29.

    ϝϦοτ w खͷಧ͘ൣғ಺Ͱϝϯςφϯε͕Ͱ͖Δ w σβΠϯͷमਖ਼͕͠΍͍͢ w ੹຿Λ෼͚Δ͜ͱ͕Ͱ͖Δ w "EBQUFSͱ7JFX)PMEFSΛάϥϑ෦෼ w

    *UFN%FDPSBUJPOΛॎ࣠Ŋԣ࣠ͷ໨੝ w *UFNͷ$MJDL&WFOUΛϋϯυϦϯά͢Δͷ΋ָ w $BOWBTͰ4DSPMMBCMFͳάϥϑΛ࡞Δͷ͸େม͚ͩͲŊ͜ΕͳΒ؆୯ͩͶ