Skip to content

Commit

Permalink
Fix a issue about Scale style when the viewpager is can loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Apr 18, 2020
1 parent d96198f commit b26dcdf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ IndicatorView supports three Indicator Styles and three Indicator Slide mode now
| NORMAL| ![CIRCLE_NORMAL](https://github.com/zhpanvip/Resource/blob/master/image/indicator/slide_circle_normal.gif) | ![DASH_NORMAL](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_dash_normal.gif) | ![ROUND_RECT_NORMAL](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_round_rect_normal.gif) |
| SMOOTH| ![CIRCLE_SMOOTH](https://github.com/zhpanvip/Resource/blob/master/image/indicator/slide_circle_smooth.gif) | ![DASH_SMOOTH](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_dash_smooth.gif) | ![ROUND_RECT_SMOOTH](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_round_rect_smooth.gif) |
| WORM| ![CIRCLE_WORM](https://github.com/zhpanvip/Resource/blob/master/image/indicator/slide_circle_worm.gif) | ![DASH_WORM](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_dash_worm.gif) | ![ROUND_WORM](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_round_rect_worm.gif) |
| COLOR| ![CIRCLE_WORM](https://github.com/zhpanvip/Resource/blob/master/image/indicator/slide_circle_color.gif) | ![DASH_WORM](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_dash_color.gif) | ![ROUND_WORM](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_round_rect_color.gif) |
| SCALE| ![CIRCLE_WORM](https://github.com/zhpanvip/Resource/blob/master/image/indicator/slide_circle_scale.gif) | ![DASH_WORM](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_dash_scale.gif) | ![ROUND_WORM](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_round_rect_scale.gif) |
| COLOR| ![CIRCLE_COLOR](https://github.com/zhpanvip/Resource/blob/master/image/indicator/slide_circle_color.gif) | ![DASH_COLOR](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_dash_color.gif) | ![ROUND_COLOR](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_round_rect_color.gif) |
| SCALE| ![CIRCLE_SCALE](https://github.com/zhpanvip/Resource/blob/master/image/indicator/slide_circle_scale.gif) | ![DASH_SCALE](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_dash_scale.gif) | ![ROUND_SCALE](https://github.com/zhpanvip/Resource/blob/master/image/indicator/style_round_rect_scale.gif) |

[More information click here.](https://github.com/zhpanvip/BannerViewPager)

## API

| Method | Description | Default |
|--|--|--|
| setIndicatorStyle(Int) | set indicator style | enum(CIRCLE, DASHROUND_RECT) default CIRCLE |
| setIndicatorStyle(Int) | set indicator style | enum(CIRCLEDASHROUND_RECT) default CIRCLE |
| setSliderColor(normalColor: Int,selectedColor: Int)| set indicator slider color |normalColor:color of indicator dot not selected, default value "#8C6C6D72", checkedColor:color of indicator selected default value is "#8C18171C" |
| setSlideMode(slideMode: Int) | set indicator slide mode | enum(NORMAL;SMOOTH;WORM),default value NORMAL |
| setSlideMode(slideMode: Int) | set indicator slide mode | enum(NORMAL;SMOOTH;WORM;COLOR;SCALE),default value NORMAL |
| setSliderWidth(indicatorWidth:Int) | set indicator slider width,if it's Circle indicator the parameter is diameter of circle | default value is 8dp|
| setSliderWidth(normalWidth Int , checkWidth Int) | set indicator slider width,if is circle style,the width is diameter of circle | default is 8dp |
| setIndicatorHeight(indicatorHeight Int) | set indicator hight,it's only used when the indicator style is DASH or ROUND_RECT | default value is normalIndicatorWidth/2 |
| setSliderGap(indicatorMargin Int ) | set the gap of indicator slider| default value is indicator slider width(or the diameter of circle)|
| setupWithViewPager(ViewPager) | To link a IndicatorView with a ViewPager together. | |
| setupWithViewPager(ViewPager2) | To link a IndicatorView with a ViewPager2 together. | |
| setupWithViewPager(ViewPager) | To link IndicatorView with ViewPager together. | |
| setupWithViewPager(ViewPager2) | To link IndicatorView with ViewPager2 together. | |


## Usage
Expand Down Expand Up @@ -84,8 +84,6 @@ latestVersion:[![latestVersion](https://jitpack.io/v/zhpanvip/viewpagerindicator
</RelativeLayout>
```



### 3.Use IndicatorView with ViewPager/ViewPager2:


Expand Down
24 changes: 13 additions & 11 deletions indicator/src/main/java/com/zhpan/indicator/drawer/CircleDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,20 @@ class CircleDrawer internal constructor(indicatorOptions: IndicatorOptions) : Ba
val slideProgress = mIndicatorOptions.slideProgress
val coordinateX = IndicatorUtils.getCoordinateX(mIndicatorOptions, maxWidth, currentPosition)
val coordinateY = IndicatorUtils.getCoordinateY(maxWidth)
if (slideProgress < 1) {
val evaluate = argbEvaluator?.evaluate(slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor)
mPaint.color = (evaluate as Int)
drawCircle(canvas, coordinateX, coordinateY, mIndicatorOptions.normalSliderWidth / 2)
var evaluate = argbEvaluator?.evaluate(slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor)
mPaint.color = (evaluate as Int)
drawCircle(canvas, coordinateX, coordinateY, mIndicatorOptions.normalSliderWidth / 2)

// 绘制可循环的ViewPager指示器渐变
evaluate = argbEvaluator?.evaluate(1 - slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor)
mPaint.color = evaluate as Int
val nextCoordinateX = if (currentPosition == mIndicatorOptions.pageSize - 1) {
IndicatorUtils.getCoordinateX(mIndicatorOptions, maxWidth, 0)
} else {
coordinateX + mIndicatorOptions.sliderGap + mIndicatorOptions.normalSliderWidth
}
if (currentPosition != mIndicatorOptions.pageSize - 1)
if (slideProgress > 0) {
val evaluate = argbEvaluator?.evaluate(1 - slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor)
mPaint.color = evaluate as Int
val nextCoordinateX = coordinateX + mIndicatorOptions.sliderGap + mIndicatorOptions.normalSliderWidth
drawCircle(canvas, nextCoordinateX, coordinateY, mIndicatorOptions.checkedSliderWidth / 2)
}
drawCircle(canvas, nextCoordinateX, coordinateY, mIndicatorOptions.checkedSliderWidth / 2)

}

private fun drawScaleSlider(canvas: Canvas) {
Expand Down
23 changes: 18 additions & 5 deletions indicator/src/main/java/com/zhpan/indicator/drawer/RectDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,33 @@ open class RectDrawer internal constructor(indicatorOptions: IndicatorOptions) :
when {
i < currentPosition -> {
mPaint.color = normalColor
val left = i * minWidth + i * indicatorGap
val left: Float = if (currentPosition == mIndicatorOptions.pageSize - 1) {
(i * minWidth + i * indicatorGap) + (maxWidth - minWidth) * mIndicatorOptions.slideProgress
} else {
(i * minWidth + i * indicatorGap)
}
mRectF.set(left, 0f, left + minWidth, sliderHeight)
drawRoundRect(canvas, sliderHeight, sliderHeight)
}
i == currentPosition -> {
mPaint.color = mIndicatorOptions.checkedSliderColor
val slideProgress = mIndicatorOptions.slideProgress
if (slideProgress < 1) {
if (currentPosition == mIndicatorOptions.pageSize - 1) {
val evaluate = argbEvaluator?.evaluate(slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor)
mPaint.color = (evaluate as Int)
val left = i * minWidth + i * indicatorGap
val right = left + minWidth + (maxWidth - minWidth) * (1 - slideProgress)
val right = (mIndicatorOptions.pageSize - 1) * (minWidth + mIndicatorOptions.sliderGap) + maxWidth
val left = right - maxWidth + (maxWidth - minWidth) * (slideProgress)
mRectF.set(left, 0f, right, sliderHeight)
drawRoundRect(canvas, sliderHeight, sliderHeight)
} else {
if (slideProgress < 1) {
val evaluate = argbEvaluator?.evaluate(slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor)
mPaint.color = (evaluate as Int)
val left = i * minWidth + i * indicatorGap
val right = left + minWidth + (maxWidth - minWidth) * (1 - slideProgress)
mRectF.set(left, 0f, right, sliderHeight)
drawRoundRect(canvas, sliderHeight, sliderHeight)
}
}

if (currentPosition == mIndicatorOptions.pageSize - 1) {
Expand All @@ -82,7 +95,7 @@ open class RectDrawer internal constructor(indicatorOptions: IndicatorOptions) :
}
}
else -> {
if ((currentPosition + 1 != i || mIndicatorOptions.slideProgress == 0f)) {
if ((currentPosition + 1 != i || mIndicatorOptions.slideProgress == 0f)) { // 避免多余绘制
mPaint.color = normalColor
val left = i * minWidth + i * indicatorGap + (maxWidth - minWidth)
mRectF.set(left, 0f, left + minWidth, sliderHeight)
Expand Down

0 comments on commit b26dcdf

Please sign in to comment.