Skip to content

Commit

Permalink
added contradicting dots indicator type
Browse files Browse the repository at this point in the history
  • Loading branch information
ByBus committed Dec 29, 2023
1 parent 0b299bf commit 86a2820
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 8 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ allprojects {

```
dependencies {
implementation 'com.github.ByBus:dots-tab-layout:0.8.0'
implementation 'com.github.ByBus:dots-tab-layout:0.9.0'
}
```

Expand All @@ -30,7 +30,7 @@ dependencies {
#### 1. Add DotsTabLayout view to your layout
```
<host.capitalquiz.dotstablayout.DotsTabLayout
android:id="@+id/tab_layout_dots"
android:id="@+id/dotstab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
Expand Down Expand Up @@ -86,6 +86,11 @@ override fun onCreate(savedInstanceState: Bundle?) {

---

#### contractingDotIndicator
![](images/contractingDotIndicator.gif?raw=true)

---

#### crawlingDotIndicator
![](images/crawlingDotIndicator.gif?raw=true)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package host.capitalquiz.dotstablayoutexample

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import host.capitalquiz.dotstablayout.DotsTabLayout
Expand All @@ -13,7 +14,11 @@ class MainActivity : AppCompatActivity() {
val pager = findViewById<ViewPager2>(R.id.pager)
pager.adapter = FragmentAdapter(supportFragmentManager, lifecycle)

val dotsLayout = findViewById<DotsTabLayout>(R.id.tab_layout_dots)
val dotsLayout = findViewById<DotsTabLayout>(R.id.dotstab_layout)
dotsLayout.attachTo(pager, this)

findViewById<Button>(R.id.nextPageButton).setOnClickListener {
pager.currentItem++
}
}
}
17 changes: 13 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/tab_layout_dots"
app:layout_constraintBottom_toTopOf="@id/dotstab_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<host.capitalquiz.dotstablayout.DotsTabLayout
android:id="@+id/tab_layout_dots"
android:id="@+id/dotstab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
Expand All @@ -29,8 +29,17 @@
app:dotsHorizontalPadding="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:indicatorType="growingDotIndicator"
app:layout_constraintBottom_toTopOf="@id/nextPageButton"
app:indicatorType="contractingDotIndicator"
app:dotsClickable="true"
tools:dotsNumber="6" />

<Button
android:id="@+id/nextPageButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 3 additions & 1 deletion dots-tab-layout/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {

defaultConfig {
minSdk = 24
version = "0.5.0"
version = "0.9.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
Expand Down Expand Up @@ -50,6 +50,8 @@ afterEvaluate {
version = "${project.version}"

from(components["release"])

// "host.capitalquiz:dotstablayout:0.8.0"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import host.capitalquiz.dotstablayout.indicatordrawer.JumpingDotIndicator
import host.capitalquiz.dotstablayout.indicatordrawer.MovingDotIndicator
import host.capitalquiz.dotstablayout.indicatordrawer.MovingLineIndicator
import host.capitalquiz.dotstablayout.indicatordrawer.CompactJumpingDotIndicator
import host.capitalquiz.dotstablayout.indicatordrawer.ContractingDotIndicator
import host.capitalquiz.dotstablayout.indicatordrawer.FadingDotIndicator
import host.capitalquiz.dotstablayout.indicatordrawer.GrowingDotIndicator
import host.capitalquiz.dotstablayout.indicatordrawer.SwappingDotIndicator
Expand Down Expand Up @@ -50,6 +51,9 @@ internal enum class IndicatorDrawerType {
},
GROWING_DOT_INDICATOR {
override fun create(dotHolder: DotHolder) = GrowingDotIndicator(dotHolder)
},
CONTRACTING_DOT_INDICATOR {
override fun create(dotHolder: DotHolder) = ContractingDotIndicator(dotHolder)
}
;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package host.capitalquiz.dotstablayout.indicatordrawer

import android.graphics.Canvas
import android.graphics.Paint
import host.capitalquiz.dotstablayout.DotHolder
import host.capitalquiz.dotstablayout.DragState
import kotlin.math.abs

internal class ContractingDotIndicator(params: DotHolder) : BaseIndicator(params) {
private val radius = params.dotSize() / 2 - params.dotStrokeWidth() + 1f
override fun draw(canvas: Canvas, paint: Paint, dragState: DragState) {
super.draw(canvas, paint, dragState)
dragState.indicatorPosition { _, y, distanceFraction ->
val current = params.currentDot()
val next = params.nextDot()
val currentCenter = current.left + current.width / 2f
val fraction = abs(distanceFraction)
if (fraction != 0f) {
canvas.drawCircle(currentCenter, y, radius * (1f - fraction), paint)
if (current != next) {
val nextCenter = next.left + next.width / 2f
canvas.drawCircle(nextCenter, y, radius * fraction, paint)
}
} else {
canvas.drawCircle(currentCenter, y, radius, paint)
}
}
}

override fun durationPerDot(dotPosition: Int): Long = 300L
}
1 change: 1 addition & 0 deletions dots-tab-layout/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<enum name="fadingDotIndicator" value="9"/>
<enum name="swappingDotIndicator" value="10"/>
<enum name="growingDotIndicator" value="11"/>
<enum name="contractingDotIndicator" value="12"/>
</attr>
</declare-styleable>
</resources>
Binary file added images/contractingDotIndicator.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 86a2820

Please sign in to comment.