From 45651efceb9cf85b23a3f0d2ee60b76baada04a8 Mon Sep 17 00:00:00 2001 From: Chahine Date: Mon, 12 Feb 2018 14:01:35 -0500 Subject: [PATCH] Feature - Performance Improvements (#9) --- .../pageindicator/sample/MainActivity.kt | 2 +- dependencies.gradle | 2 +- pageindicator/build.gradle | 4 +-- .../chahinem/pageindicator/PageIndicator.kt | 25 +++++++++++++------ settings.gradle | 3 ++- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/chahinem/pageindicator/sample/MainActivity.kt b/app/src/main/java/com/chahinem/pageindicator/sample/MainActivity.kt index 3371b0a..627d21d 100644 --- a/app/src/main/java/com/chahinem/pageindicator/sample/MainActivity.kt +++ b/app/src/main/java/com/chahinem/pageindicator/sample/MainActivity.kt @@ -34,7 +34,7 @@ class MainActivity : AppCompatActivity() { pagerPageIndicator.attachTo(pager) // Manual - manualPageIndicator.count = 4 + manualPageIndicator.count = 777 leftBtn.setOnClickListener { manualPageIndicator.swipePrevious() } rightBtn.setOnClickListener { manualPageIndicator.swipeNext() } } diff --git a/dependencies.gradle b/dependencies.gradle index 26620a5..bda8f79 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -5,7 +5,7 @@ ext { targetSdkVersion = 27 buildToolsVersion = '27.0.2' gradleVersion = '3.0.1' - kotlinVersion = '1.2.20' + kotlinVersion = '1.2.21' supportLibraryVersion = '27.0.2' diff --git a/pageindicator/build.gradle b/pageindicator/build.gradle index 8edc59f..566b5b7 100644 --- a/pageindicator/build.gradle +++ b/pageindicator/build.gradle @@ -3,8 +3,8 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'com.github.dcendents.android-maven' -group = 'com.github.chahinem.pageindicator' -version = '0.2.3' +group = 'com.github.chahinem' +version = '0.2.4' androidExtensions { experimental = true } diff --git a/pageindicator/src/main/java/com/chahinem/pageindicator/PageIndicator.kt b/pageindicator/src/main/java/com/chahinem/pageindicator/PageIndicator.kt index d8c00ef..5500caa 100644 --- a/pageindicator/src/main/java/com/chahinem/pageindicator/PageIndicator.kt +++ b/pageindicator/src/main/java/com/chahinem/pageindicator/PageIndicator.kt @@ -103,13 +103,15 @@ class PageIndicator @JvmOverloads constructor( super.onDraw(canvas) var paddingStart = initialPadding - // FIXME: do not iterate on the entire list but only a subset of it - dotManager?.dots?.forEachIndexed { index, dot -> + val (start, end) = getDrawingRange() + + paddingStart += (dotSize + dotSpacing) * start + (start until end).forEach { canvas?.drawCircle( paddingStart + dotSize / 2f - scrollAmount, dotSize / 2f, - dotSizes[index] / 2f, - when (dot) { + dotSizes[it] / 2f, + when (dotManager?.dots?.get(it)) { BYTE_6 -> selectedPaint else -> defaultPaint }) @@ -180,10 +182,10 @@ class PageIndicator @JvmOverloads constructor( private fun animateDots() { dotManager?.let { - // FIXME: do not iterate on the entire list but only a subset of it - it.dots.forEachIndexed { index, dot -> + val (start, end) = getDrawingRange() + (start until end).forEach { index -> dotAnimators[index].cancel() - dotAnimators[index] = ValueAnimator.ofInt(dotSizes[index], it.dotSizeFor(dot)) + dotAnimators[index] = ValueAnimator.ofInt(dotSizes[index], it.dotSizeFor(it.dots[index])) .apply { duration = animDuration interpolator = DEFAULT_INTERPOLATOR @@ -197,6 +199,14 @@ class PageIndicator @JvmOverloads constructor( } } + private fun getDrawingRange(): Pair { + val start = Math.max(0, (dotManager?.selectedIndex ?: 0) - MOST_VISIBLE_COUNT) + val end = Math.min( + dotManager?.dots?.size ?: 0, + (dotManager?.selectedIndex ?: 0) + MOST_VISIBLE_COUNT) + return Pair(start, end) + } + companion object { private const val BYTE_6 = 6.toByte() private const val BYTE_5 = 5.toByte() @@ -205,6 +215,7 @@ class PageIndicator @JvmOverloads constructor( private const val BYTE_2 = 2.toByte() private const val BYTE_1 = 1.toByte() + private const val MOST_VISIBLE_COUNT = 10 private const val DEFAULT_ANIM_DURATION = 200 private val DEFAULT_INTERPOLATOR = DecelerateInterpolator() diff --git a/settings.gradle b/settings.gradle index b26d93a..b948c16 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,3 @@ include ':app' -include ':pageindicator' \ No newline at end of file +include ':pageindicator' +project(':pageindicator').name = 'pageindicator'