Skip to content

Added selectedDotDrawable customisation #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ indicator.attachToPager(pager, new ViewPagerAttacher());
| spi_dotMinimumSize | The minimum dot size for the corner dots. This size is lower or equal to ```spi_dotSize``` and greater or equal to the internal calculation for the corner dots. | Internal calculation based on ```spi_dotSize```, ```spi_dotSelectedSize``` and ```spi_dotSpacing``` |
| spi_orientation | Visible orientation of the dots | LinearLayoutManager.HORIZONTAL |
| spi_firstDotDrawable | Custom drawable of the first dot, color is tinted and size is changed like standard dots. If first dot is also the last one then this value is taken into account. | `null` |
| spi_lastDotDrawable | Custom drawable of the last dot, color is tinted and size is changed like standard dots. | `null` |
| spi_lastDotDrawable | Custom drawable of the last dot, color is tinted and size is changed like standard dots. | `null` |
| spi_selectedDotDrawable | Custom drawable of the selected dot, color is tinted and size is changed like standard dots. | `null` |

## TODO
1. Some extreme customizations may work incorrect.
Expand Down
5 changes: 5 additions & 0 deletions demo/src/main/res/drawable/baseline_play_arrow_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M8,5v14l11,-7z"/>
</vector>
3 changes: 2 additions & 1 deletion demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/pager_title2"
app:spi_dotColor="@color/dotNormal"
app:spi_dotSelectedColor="@color/dotHighlight" />
app:spi_dotSelectedColor="@color/dotHighlight"
app:spi_selectedDotDrawable="@drawable/baseline_play_arrow_24"/>

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -62,6 +63,10 @@ public class ScrollingPagerIndicator extends View {
@Nullable
private final Drawable lastDotDrawable;

@Nullable
private Drawable selectedDotDrawable;


private boolean looped;

private Runnable attachRunnable;
Expand Down Expand Up @@ -99,6 +104,7 @@ public ScrollingPagerIndicator(Context context, @Nullable AttributeSet attrs, in

firstDotDrawable = attributes.getDrawable(R.styleable.ScrollingPagerIndicator_spi_firstDotDrawable);
lastDotDrawable = attributes.getDrawable(R.styleable.ScrollingPagerIndicator_spi_lastDotDrawable);
selectedDotDrawable = attributes.getDrawable(R.styleable.ScrollingPagerIndicator_spi_selectedDotDrawable);
attributes.recycle();

paint = new Paint();
Expand Down Expand Up @@ -159,6 +165,11 @@ public void setSelectedDotColor(@ColorInt int color) {
invalidate();
}

public void setSelectedDotDrawable(Drawable drawable) {
this.selectedDotDrawable = drawable;
invalidate();
}

/**
* Maximum number of dots which will be visible at the same time.
* If pager has more pages than visible_dot_count, indicator will scroll to show extra dots.
Expand Down Expand Up @@ -561,24 +572,28 @@ protected void onDraw(Canvas canvas) {

paint.setColor(calculateDotColor(scale));
final Drawable dotDrawable;
if (i == firstVisibleDotPos) {
if (scale == 1f) {
dotDrawable = selectedDotDrawable;
} else if (i == firstVisibleDotPos) {
dotDrawable = firstDotDrawable;
} else if (i == lastVisibleDotPos) {
dotDrawable = lastDotDrawable;
} else {
dotDrawable = null;
}
if (dotDrawable != null) {
int size = dotDrawable == selectedDotDrawable ? dotSelectedSize : dotSelectedSize / 2;

if (orientation == LinearLayoutManager.HORIZONTAL) {
dotDrawable.setBounds((int) (dot - visibleFramePosition - dotSelectedSize / 2),
getMeasuredHeight() / 2 - dotSelectedSize / 2,
(int) (dot - visibleFramePosition + dotSelectedSize / 2),
getMeasuredHeight() / 2 + dotSelectedSize / 2);
dotDrawable.setBounds((int) (dot - visibleFramePosition - size),
getMeasuredHeight() / 2 - size,
(int) (dot - visibleFramePosition + size),
getMeasuredHeight() / 2 + size);
} else {
dotDrawable.setBounds(getMeasuredWidth() / 2 - dotSelectedSize / 2,
(int) (dot - visibleFramePosition - dotSelectedSize / 2),
getMeasuredWidth() / 2 + dotSelectedSize / 2,
(int) (dot - visibleFramePosition + dotSelectedSize / 2));
dotDrawable.setBounds(getMeasuredWidth() / 2 - size,
(int) (dot - visibleFramePosition - size),
getMeasuredWidth() / 2 + size,
(int) (dot - visibleFramePosition + size));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dotDrawable.setTint(paint.getColor());
Expand Down
1 change: 1 addition & 0 deletions scrollingpagerindicator/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
</attr>
<attr name="spi_firstDotDrawable" format="reference" />
<attr name="spi_lastDotDrawable" format="reference" />
<attr name="spi_selectedDotDrawable" format="reference" />
</declare-styleable>
</resources>