Skip to content

Commit

Permalink
support RTL direction for horizontal mode (#43)
Browse files Browse the repository at this point in the history
* support rtl for horizontal mode

* bump version

* update change-log

* add option to disabling auto rtl direction
  • Loading branch information
beigirad authored Aug 19, 2022
1 parent fddd00d commit f348756
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 1.2.2

#### Fixes
- support RTL direction in horizontal mode when the view is RTL [#29](https://github.com/Tinkoff/ScrollingPagerIndicator/issues/29)
- turn off RTL direction by `setAutoRtl(false)`

#### Changes
#### Additions

## 1.2.1

#### Fixes
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android.useAndroidX=true
# org.gradle.parallel=true
android.useAndroidX=true

VERSION_NAME=1.2.1
VERSION_CODE=11
VERSION_NAME=1.2.2
VERSION_CODE=12
GROUP=ru.tinkoff.scrollingpagerindicator

POM_DESCRIPTION=Pager indicator inspired by Instagram. Lightweight and easy to set up.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
Expand Down Expand Up @@ -58,6 +59,7 @@ public class ScrollingPagerIndicator extends View {

private Runnable attachRunnable;
private PagerAttacher<?> currentAttacher;
private boolean autoRtl = true;

private boolean dotCountInitialized;

Expand Down Expand Up @@ -314,6 +316,7 @@ public void detachFromPager() {
currentAttacher.detachFromPager();
currentAttacher = null;
attachRunnable = null;
autoRtl = true;
}
dotCountInitialized = false;
}
Expand Down Expand Up @@ -396,6 +399,17 @@ public void setCurrentPosition(int position) {
updateScaleInIdleState(position);
}

/**
* Sets Rtl direction availability when the view has Rtl direction.
* autoRtl is on by default.
*
* @param autoRtl false means rtl direction doesn't be apply even if view direction is Rtl.
*/
public void setAutoRtl(boolean autoRtl) {
this.autoRtl = autoRtl;
invalidate();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Width
Expand Down Expand Up @@ -538,7 +552,12 @@ protected void onDraw(Canvas canvas) {

paint.setColor(calculateDotColor(scale));
if (orientation == LinearLayoutManager.HORIZONTAL) {
canvas.drawCircle(dot - visibleFramePosition,
float cx = dot - visibleFramePosition;
if (autoRtl && isRtl()) {
cx = getWidth() - cx;
}

canvas.drawCircle(cx,
getMeasuredHeight() / 2,
diameter / 2,
paint);
Expand All @@ -552,6 +571,11 @@ protected void onDraw(Canvas canvas) {
}
}

private boolean isRtl() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 &&
getLayoutDirection() == LAYOUT_DIRECTION_RTL;
}

@ColorInt
private int calculateDotColor(float dotScale) {
return (Integer) colorEvaluator.evaluate(dotScale, dotColor, selectedDotColor);
Expand Down

0 comments on commit f348756

Please sign in to comment.