Skip to content

Commit

Permalink
Feature - Performance Improvements (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chahine authored Feb 12, 2018
1 parent d9e307a commit 45651ef
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
4 changes: 2 additions & 2 deletions pageindicator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -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
Expand All @@ -197,6 +199,14 @@ class PageIndicator @JvmOverloads constructor(
}
}

private fun getDrawingRange(): Pair<Int, Int> {
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()
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include ':app'
include ':pageindicator'
include ':pageindicator'
project(':pageindicator').name = 'pageindicator'

0 comments on commit 45651ef

Please sign in to comment.