Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RecyclerViewで 折れ線グラフを作る

Yuki Mima
December 12, 2019

RecyclerViewで 折れ線グラフを作る

Yuki Mima

December 12, 2019
Tweet

More Decks by Yuki Mima

Other Decks in Programming

Transcript

  1.  

  2.  

  3. 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 = [email protected] isAntiAlias = true } private val yearAndMonthPaint = Paint().apply { color = Color.parseColor("#FFFF00") textSize = [email protected] 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 }
  4. ϝϦοτ w खͷಧ͘ൣғ಺Ͱϝϯςφϯε͕Ͱ͖Δ w σβΠϯͷमਖ਼͕͠΍͍͢ w ੹຿Λ෼͚Δ͜ͱ͕Ͱ͖Δ w "EBQUFSͱ7JFX)PMEFSΛάϥϑ෦෼ w

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