Skip to content

Commit

Permalink
Fix #25 indicate the unit of setSliderWidth()
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Feb 7, 2023
1 parent 2dc051d commit 542738d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.util.AttributeSet
import android.view.View

import androidx.annotation.ColorInt
import androidx.annotation.Px
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2

Expand Down Expand Up @@ -104,7 +105,7 @@ open class BaseIndicatorView constructor(
positionOffset: Float
) {
if (mIndicatorOptions.slideMode == IndicatorSlideMode.SCALE
|| mIndicatorOptions.slideMode == IndicatorSlideMode.COLOR
|| mIndicatorOptions.slideMode == IndicatorSlideMode.COLOR
) {
setCurrentPosition(position)
setSlideProgress(positionOffset)
Expand Down Expand Up @@ -160,7 +161,7 @@ open class BaseIndicatorView constructor(
return mIndicatorOptions.checkedSliderWidth
}

fun setCheckedSlideWidth(checkedSliderWidth: Float) {
fun setCheckedSlideWidth(@Px checkedSliderWidth: Float) {
mIndicatorOptions.checkedSliderWidth = checkedSliderWidth
}

Expand Down Expand Up @@ -220,14 +221,25 @@ open class BaseIndicatorView constructor(
return this
}

fun setSliderWidth(sliderWidth: Float): BaseIndicatorView {
/**
* set width for indicator slider,unit is px.
*
* @param sliderWidth the indicator slider width,checked width equals unchecked width.
*/
fun setSliderWidth(@Px sliderWidth: Float): BaseIndicatorView {
mIndicatorOptions.setSliderWidth(sliderWidth)
return this
}

/**
* set width for indicator slider ,unit is px.
*
* @param normalSliderWidth unchecked slider radius
* @param selectedSliderWidth checked slider radius
*/
fun setSliderWidth(
normalSliderWidth: Float,
selectedSliderWidth: Float
@Px normalSliderWidth: Float,
@Px selectedSliderWidth: Float
): BaseIndicatorView {
mIndicatorOptions.setSliderWidth(normalSliderWidth, selectedSliderWidth)
return this
Expand Down
36 changes: 10 additions & 26 deletions indicator/src/main/java/com/zhpan/indicator/drawer/CircleDrawer.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
Copyright (C) 2020 zhpanvip,ViewPagerIndicator Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.zhpan.indicator.drawer

import android.graphics.Canvas
Expand All @@ -30,7 +14,7 @@ import com.zhpan.indicator.utils.IndicatorUtils
</pre> *
*/
class CircleDrawer internal constructor(indicatorOptions: IndicatorOptions) : BaseDrawer(
indicatorOptions
indicatorOptions
) {

private val rectF = RectF()
Expand Down Expand Up @@ -73,14 +57,14 @@ class CircleDrawer internal constructor(indicatorOptions: IndicatorOptions) : Ba
val coordinateX = IndicatorUtils.getCoordinateX(mIndicatorOptions, maxWidth, currentPosition)
val coordinateY = IndicatorUtils.getCoordinateY(maxWidth)
var evaluate = argbEvaluator?.evaluate(
slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor
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
1 - slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor
)
mPaint.color = evaluate as Int
val nextCoordinateX = if (currentPosition == mIndicatorOptions.pageSize - 1) {
Expand All @@ -98,7 +82,7 @@ class CircleDrawer internal constructor(indicatorOptions: IndicatorOptions) : Ba
val coordinateY = IndicatorUtils.getCoordinateY(maxWidth)
if (slideProgress < 1) {
val evaluate = argbEvaluator?.evaluate(
slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor
slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor
)
mPaint.color = (evaluate as Int)
val radius =
Expand All @@ -108,7 +92,7 @@ class CircleDrawer internal constructor(indicatorOptions: IndicatorOptions) : Ba

if (currentPosition == mIndicatorOptions.pageSize - 1) {
val evaluate = argbEvaluator?.evaluate(
slideProgress, mIndicatorOptions.normalSliderColor, mIndicatorOptions.checkedSliderColor
slideProgress, mIndicatorOptions.normalSliderColor, mIndicatorOptions.checkedSliderColor
)
mPaint.color = evaluate as Int
val nextCoordinateX = maxWidth / 2
Expand All @@ -117,7 +101,7 @@ class CircleDrawer internal constructor(indicatorOptions: IndicatorOptions) : Ba
} else {
if (slideProgress > 0) {
val evaluate = argbEvaluator?.evaluate(
slideProgress, mIndicatorOptions.normalSliderColor, mIndicatorOptions.checkedSliderColor
slideProgress, mIndicatorOptions.normalSliderColor, mIndicatorOptions.checkedSliderColor
)
mPaint.color = evaluate as Int
val nextCoordinateX =
Expand All @@ -134,7 +118,7 @@ class CircleDrawer internal constructor(indicatorOptions: IndicatorOptions) : Ba
val startCoordinateX =
IndicatorUtils.getCoordinateX(mIndicatorOptions, maxWidth, currentPosition)
val endCoordinateX = IndicatorUtils.getCoordinateX(
mIndicatorOptions, maxWidth, (currentPosition + 1) % mIndicatorOptions.pageSize
mIndicatorOptions, maxWidth, (currentPosition + 1) % mIndicatorOptions.pageSize
)
val coordinateX =
startCoordinateX + (endCoordinateX - startCoordinateX) * mIndicatorOptions.slideProgress
Expand All @@ -151,10 +135,10 @@ class CircleDrawer internal constructor(indicatorOptions: IndicatorOptions) : Ba
val startCoordinateX =
IndicatorUtils.getCoordinateX(mIndicatorOptions, maxWidth, currentPosition)
val left = startCoordinateX + (distance * (slideProgress - 0.5f) * 2.0f).coerceAtLeast(
0f
0f
) - mIndicatorOptions.normalSliderWidth / 2 + INDICATOR_PADDING
val right = startCoordinateX + (distance * slideProgress * 2f).coerceAtMost(
distance
distance
) + mIndicatorOptions.normalSliderWidth / 2 + INDICATOR_PADDING
rectF.set(left, INDICATOR_PADDING.toFloat(), right, sliderHeight + INDICATOR_PADDING)
canvas.drawRoundRect(rectF, sliderHeight, sliderHeight, mPaint)
Expand All @@ -167,7 +151,7 @@ class CircleDrawer internal constructor(indicatorOptions: IndicatorOptions) : Ba
radius: Float
) {
canvas.drawCircle(
coordinateX + INDICATOR_PADDING, coordinateY + INDICATOR_PADDING, radius, mPaint
coordinateX + INDICATOR_PADDING, coordinateY + INDICATOR_PADDING, radius, mPaint
)
}
}

0 comments on commit 542738d

Please sign in to comment.