From 54f9c3162f26149f314f452b4af4c68261be250f Mon Sep 17 00:00:00 2001 From: Riccardo Lattarulo Date: Mon, 24 Jan 2022 21:56:35 +0100 Subject: [PATCH 1/2] Improve TimelineDecorator performances --- .../timelineview/TimelineDecorator.kt | 107 ++++++++++-------- 1 file changed, 58 insertions(+), 49 deletions(-) diff --git a/TimelineView/src/main/java/com/lriccardo/timelineview/TimelineDecorator.kt b/TimelineView/src/main/java/com/lriccardo/timelineview/TimelineDecorator.kt index 0076784..85e1a79 100644 --- a/TimelineView/src/main/java/com/lriccardo/timelineview/TimelineDecorator.kt +++ b/TimelineView/src/main/java/com/lriccardo/timelineview/TimelineDecorator.kt @@ -58,91 +58,100 @@ class TimelineDecorator( override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) { c.save() - c.clipRect(parent.paddingLeft, parent.paddingTop, parent.width, parent.height-parent.paddingBottom) + c.clipRect( + parent.paddingLeft, + parent.paddingTop, + parent.width, + parent.height - parent.paddingBottom + ) - parent.children.forEach { - val itemPosition = parent.getChildAdapterPosition(it) + (parent.adapter as? TimelineAdapter).let { adapter -> + parent.children.forEach { + val itemPosition = parent.getChildAdapterPosition(it) - if (itemPosition == RecyclerView.NO_POSITION) - return + if (itemPosition == RecyclerView.NO_POSITION) + return - val timelineView = TimelineView(context = parent.context) + val timelineView = TimelineView(context = parent.context) - (parent.adapter as? TimelineAdapter ?: object : TimelineAdapter {}).run { - getTimelineViewType(itemPosition)?.let { + adapter?.getTimelineViewType(itemPosition)?.let { timelineView.viewType = it } ?: timelineView.setType(itemPosition, parent.adapter?.itemCount ?: -1) - (getIndicatorDrawable(itemPosition) ?: indicatorDrawable)?.let { + (adapter?.getIndicatorDrawable(itemPosition) ?: indicatorDrawable)?.let { timelineView.indicatorDrawable = it - } ?: (getIndicatorDrawableRes(itemPosition) ?: indicatorDrawableRes)?.let { + } ?: (adapter?.getIndicatorDrawableRes(itemPosition) ?: indicatorDrawableRes)?.let { timelineView.indicatorDrawable = ContextCompat.getDrawable(parent.context, it) } - (getIndicatorColor(itemPosition) ?: indicatorColor)?.let { + (adapter?.getIndicatorColor(itemPosition) ?: indicatorColor)?.let { timelineView.indicatorColor = it } - (getLineColor(itemPosition) ?: lineColor)?.let { + (adapter?.getLineColor(itemPosition) ?: lineColor)?.let { timelineView.lineColor = it } - (getIndicatorStyle(itemPosition) ?: indicatorStyle)?.let { + (adapter?.getIndicatorStyle(itemPosition) ?: indicatorStyle)?.let { timelineView.indicatorStyle = it } - (getLineStyle(itemPosition) ?: lineStyle)?.let { + (adapter?.getLineStyle(itemPosition) ?: lineStyle)?.let { timelineView.lineStyle = it } - (getLinePadding(itemPosition) ?: linePadding)?.let { + (adapter?.getLinePadding(itemPosition) ?: linePadding)?.let { timelineView.linePadding = it } - } - - timelineView.indicatorSize = indicatorSize - timelineView.indicatorYPosition = indicatorYPosition + timelineView.indicatorSize = indicatorSize - checkedIndicatorSize?.let { - timelineView.checkedIndicatorSize = it - } + timelineView.indicatorYPosition = indicatorYPosition - checkedIndicatorStrokeWidth.let { - timelineView.checkedIndicatorStrokeWidth = it - } - - lineDashLength?.let { - timelineView.lineDashLength = it - } + checkedIndicatorSize?.let { + timelineView.checkedIndicatorSize = it + } - lineDashGap?.let { - timelineView.lineDashGap = it - } + checkedIndicatorStrokeWidth.let { + timelineView.checkedIndicatorStrokeWidth = it + } - lineWidth?.let { - timelineView.lineWidth = it - } + lineDashLength?.let { + timelineView.lineDashLength = it + } - timelineView.measure( - View.MeasureSpec.getSize(width.toInt()), - View.MeasureSpec.getSize(it.measuredHeight) - ) + lineDashGap?.let { + timelineView.lineDashGap = it + } - c.save() - when (position) { - Position.Left -> { - c.translate(padding+parent.paddingLeft, it.top.toFloat()) - timelineView.layout(0, 0, timelineView.measuredWidth, it.measuredHeight) + lineWidth?.let { + timelineView.lineWidth = it } - Position.Right -> { - c.translate(it.measuredWidth + padding + parent.paddingLeft, it.top.toFloat()) - timelineView.layout(0, 0, timelineView.measuredWidth, it.measuredHeight) + + timelineView.measure( + View.MeasureSpec.getSize(width.toInt()), + View.MeasureSpec.getSize(it.measuredHeight) + ) + + c.save() + when (position) { + Position.Left -> { + c.translate(padding + parent.paddingLeft, it.top.toFloat()) + timelineView.layout(0, 0, timelineView.measuredWidth, it.measuredHeight) + } + Position.Right -> { + c.translate( + it.measuredWidth + padding + parent.paddingLeft, + it.top.toFloat() + ) + timelineView.layout(0, 0, timelineView.measuredWidth, it.measuredHeight) + } } + timelineView.draw(c) + c.restore() } - timelineView.draw(c) - c.restore() } + c.restore() } } \ No newline at end of file From 63644e840250cb85906fa4e5a935330dc412dd6c Mon Sep 17 00:00:00 2001 From: Riccardo Lattarulo Date: Sat, 29 Jan 2022 09:51:31 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6dc5135..19019e2 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ allprojects { ```gradle dependencies { - implementation 'com.github.lriccardo:TimelineView:1.1.1' + implementation 'com.github.lriccardo:TimelineView:1.1.2' } ```