Skip to content

Commit bd89af3

Browse files
committed
Merge branch 'release/0.1.1'
2 parents 22e799c + 09e46a6 commit bd89af3

File tree

11 files changed

+97
-27
lines changed

11 files changed

+97
-27
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
![Image](/pic/banner.png)
12
# IndicatorDecorator
23
Indicator decorator is an indicator that can be used in ViewPager2 and RecyclerView.
34

4-
## What's New in 0.1.0? :tada:
5+
## What's New in 0.1.1? :tada:
56
- [Feature] Indicators to overlap. Use`isOverlap` (#2)
7+
- [Feature] Indicator background setting method added (#4)
68

79
## How to Use
810

911
### Gradle
1012
```groovy
1113
dependencies {
12-
implementation 'xyz.sangcomz:indicatordecorator:0.1.0'
14+
implementation 'xyz.sangcomz:indicatordecorator:0.1.1'
1315
}
1416
```
1517
### Usage
@@ -30,6 +32,11 @@ Indicator decorator is an indicator that can be used in ViewPager2 and RecyclerV
3032
| indicatorItemPadding | Padding between indicators | 8DP |
3133
| indicatorShape | Indicator shape | CircleIndicator |
3234
| isOverlap | Can overlap | false |
35+
| isShowBackground |Background visibility | false |
36+
| backgroundColor | Background Color | WHITE |
37+
| backgroundCornerRadius | Background Corner Radius | 8DP |
38+
| backgroundSideOffset | Background side offset | 16DP |
39+
|backgroundTopAndBottomOffset| Background top and bottom offset | 4DP |
3340

3441
#### Support Indicator Shape
3542

app/src/main/java/xyz/sangcomz/indicatordecoratorsample/MainActivity.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@ class MainActivity : AppCompatActivity() {
5555
super.onCreate(savedInstanceState)
5656
setContentView(R.layout.activity_main)
5757

58-
viewPager1.adapter = adapter1
59-
viewPager1.addItemDecoration(IndicatorItemDecoration().apply {
60-
indicatorShape = CircleIndicator().apply {
61-
isOverlap = true
62-
colorActive = ContextCompat.getColor(this@MainActivity, R.color.colorPrimaryDark)
63-
}
58+
recyclerView.adapter = adapter1
59+
PagerSnapHelper().attachToRecyclerView(recyclerView)
60+
recyclerView.addItemDecoration(IndicatorItemDecoration().apply {
61+
indicatorShape = DrawableIndicator(
62+
ContextCompat.getDrawable(
63+
this@MainActivity,
64+
R.drawable.ic_grade_red_24dp
65+
)!!,
66+
ContextCompat.getDrawable(this@MainActivity, R.drawable.ic_grade_gray_24dp)!!
67+
)
6468
})
6569

6670
viewPager2.adapter = adapter2
@@ -70,17 +74,12 @@ class MainActivity : AppCompatActivity() {
7074
}
7175
})
7276

73-
recyclerView.adapter = adapter3
74-
PagerSnapHelper().attachToRecyclerView(recyclerView)
75-
recyclerView.addItemDecoration(IndicatorItemDecoration().apply {
76-
indicatorShape = DrawableIndicator(
77-
ContextCompat.getDrawable(
78-
this@MainActivity,
79-
R.drawable.ic_grade_red_24dp
80-
)!!,
81-
ContextCompat.getDrawable(this@MainActivity, R.drawable.ic_grade_gray_24dp)!!
82-
).apply {
83-
width = 64f
77+
viewPager1.adapter = adapter3
78+
viewPager1.addItemDecoration(IndicatorItemDecoration().apply {
79+
indicatorShape = CircleIndicator().apply {
80+
isOverlap = true
81+
colorActive = ContextCompat.getColor(this@MainActivity, R.color.colorPrimaryDark)
82+
isShowBackground = true
8483
}
8584
})
8685

gradle/release.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ if (project.rootProject.file('local.properties').isFile()) {
9898
tagName = version
9999
targetCommitish = 'master'
100100
body = """## Release Note
101-
* Release!
101+
* [Feature] Indicators to overlap. Use`isOverlap` (#2)
102+
8 [Feature] Indicator background setting method added (#4)
102103
"""
103104
name = version
104105
}

indicatordecorator/src/main/java/xyz/sangcomz/indicatordecorator/IndicatorItemDecoration.kt

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package xyz.sangcomz.indicatordecorator
22

3-
import android.graphics.Canvas
4-
import android.graphics.Rect
3+
import android.graphics.*
54
import android.view.View
65
import androidx.recyclerview.widget.LinearLayoutManager
76
import androidx.recyclerview.widget.RecyclerView
@@ -30,6 +29,29 @@ class IndicatorItemDecoration : RecyclerView.ItemDecoration() {
3029
* Indicator shape
3130
*/
3231
var isOverlap: Boolean = false
32+
33+
private val backgroundPaint = Paint()
34+
/**
35+
* Background visibility
36+
*/
37+
var isShowBackground = false
38+
/**
39+
* Background Color
40+
*/
41+
var backgroundColor = Color.WHITE
42+
/**
43+
* Background Corner Radius
44+
*/
45+
var backgroundCornerRadius = 8.toDP()
46+
/**
47+
* Background side offset
48+
*/
49+
var backgroundSideOffset = 16.toDP().toInt()
50+
/**
51+
* Background top And bottom offset
52+
*/
53+
var backgroundTopAndBottomOffset = 4.toDP().toInt()
54+
3355
/**
3456
* Indicator shape
3557
* By default it has a circle of radius 4.
@@ -55,6 +77,13 @@ class IndicatorItemDecoration : RecyclerView.ItemDecoration() {
5577
if (isOverlap) parent.height - indicatorShape.getIndicatorHeight() - bottomOffset
5678
else parent.height - (indicatorShape.getIndicatorHeight() / 2) - bottomOffset
5779

80+
if (isShowBackground)
81+
drawBackground(
82+
c,
83+
indicatorTotalWidth,
84+
indicatorStartX,
85+
parent.height.toFloat() - bottomOffset
86+
)
5887

5988
drawInactiveIndicators(c, indicatorStartX, indicatorPosY, itemCount)
6089

@@ -68,8 +97,24 @@ class IndicatorItemDecoration : RecyclerView.ItemDecoration() {
6897
indicatorPosY
6998
)
7099
}
100+
}
71101

72-
102+
private fun drawBackground(
103+
c: Canvas,
104+
totalWidth: Float,
105+
startX: Float,
106+
bottomY: Float
107+
) {
108+
val rectF = RectF()
109+
110+
backgroundPaint.color = backgroundColor
111+
rectF.set(
112+
startX + indicatorShape.getIndicatorWidth() / 2 - indicatorShape.drawStartPosition - backgroundSideOffset,
113+
bottomY - indicatorShape.getIndicatorHeight() - indicatorShape.drawStartPosition - backgroundTopAndBottomOffset,
114+
startX + totalWidth + indicatorShape.getIndicatorWidth() / 2 - indicatorShape.drawStartPosition + backgroundSideOffset,
115+
bottomY - indicatorShape.drawStartPosition + backgroundTopAndBottomOffset
116+
)
117+
c.drawRoundRect(rectF, backgroundCornerRadius, backgroundCornerRadius, backgroundPaint)
73118
}
74119

75120
private fun drawInactiveIndicators(
@@ -106,6 +151,7 @@ class IndicatorItemDecoration : RecyclerView.ItemDecoration() {
106151
)
107152
}
108153

154+
109155
override fun getItemOffsets(
110156
outRect: Rect,
111157
view: View,
@@ -114,6 +160,9 @@ class IndicatorItemDecoration : RecyclerView.ItemDecoration() {
114160
) {
115161
super.getItemOffsets(outRect, view, parent, state)
116162
if (!isOverlap)
117-
outRect.bottom = (topOffset + bottomOffset + indicatorShape.getIndicatorHeight()).toInt()
163+
outRect.bottom = getTotalHeight().toInt()
118164
}
165+
166+
private fun getTotalHeight() = topOffset + bottomOffset + indicatorShape.getIndicatorHeight()
167+
119168
}

indicatordecorator/src/main/java/xyz/sangcomz/indicatordecorator/shape/CircleIndicator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import xyz.sangcomz.indicatordecorator.ext.toDP
77

88
class CircleIndicator : IndicatorShape {
99
private val paint = Paint().apply { isAntiAlias = true }
10+
override val drawStartPosition: Float
11+
get() = radius
1012
var colorActive: Int = Color.CYAN
1113
var colorInactive: Int = Color.GRAY
1214
var radius: Float = 4.toDP()

indicatordecorator/src/main/java/xyz/sangcomz/indicatordecorator/shape/DrawableIndicator.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ class DrawableIndicator(
88
drawableActive: Drawable,
99
drawableInActive: Drawable
1010
) : IndicatorShape {
11-
11+
override val drawStartPosition = 0f
1212
override var scaleFactor: Float = 1.2f
13+
1314
var width: Float = drawableActive.intrinsicWidth.toFloat()
1415
var height: Float = drawableActive.intrinsicHeight.toFloat()
1516
var drawableActive = drawableActive
1617
var drawableInActive = drawableInActive
1718

19+
1820
override fun inactiveIndicatorDraw(c: Canvas, x: Float, y: Float) {
1921
drawableInActive.setBounds(
2022
x.toInt(),

indicatordecorator/src/main/java/xyz/sangcomz/indicatordecorator/shape/IndicatorShape.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ interface IndicatorShape {
88
*/
99
var scaleFactor: Float
1010

11+
/**
12+
* The starting coordinate that is actually drawn
13+
* For example, when drawing a square, draw a circle from the x coordinate to the width and a circle the radius of both sides of the x coordinate.
14+
*/
15+
val drawStartPosition: Float
1116
/**
1217
* To draw inactive indicator
1318
*/

indicatordecorator/src/main/java/xyz/sangcomz/indicatordecorator/shape/SquareIndicator.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import android.graphics.Paint
66
import xyz.sangcomz.indicatordecorator.ext.toDP
77

88
class SquareIndicator : IndicatorShape {
9+
override val drawStartPosition = 0f
10+
911
private val paint = Paint().apply { isAntiAlias = true }
12+
1013
var colorActive: Int = Color.BLACK
1114
var colorInactive: Int = Color.GRAY
1215
var width = 8.toDP()
16+
17+
1318
override var scaleFactor = 1.2f
1419
override fun inactiveIndicatorDraw(c: Canvas, x: Float, y: Float) {
1520
paint.color = colorInactive

pic/banner.png

214 KB
Loading

pic/sample.gif

53.1 KB
Loading

0 commit comments

Comments
 (0)