Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
some RTL bugs has been fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhart committed Sep 23, 2018
1 parent 249ded8 commit 798246e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void onClick(View v) {
for (int i = 0; i < 5; i++) {
steps.add("Step " + (i + 1));
}
steps.set(steps.size() - 1, steps.get(steps.size() - 1) + " last one");
stepView.setSteps(steps);

final StepView stepView2 = findViewById(R.id.step_view_2);
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<string name="main_list_item_customise_title">Customise</string>
<string name="main_list_item_customise_subtitle">Customise StepView</string>
<string name="main_list_item_rtl_title">RTL</string>
<string name="main_list_item_rtl_subtitle">An example with Arabic language \n(For the sake of simplicity, RTL is forced in layout)</string>
<string name="main_list_item_rtl_subtitle">An example with Arabic language \n(You need to force RTL or select RTL locale in order to see the effect)</string>

<string name="dummy_text">Performance profilers
Android Studio provides performance profilers so you can more easily track your app’s memory and CPU usage, find deallocated objects, locate memory leaks, optimize graphics performance, and analyze network requests. With your app running on a device or emulator, open the Android Profiler tab.
Expand Down
100 changes: 47 additions & 53 deletions stepview/src/main/java/com/shuhart/stepview/StepView.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ private void applyStyles(Context context, AttributeSet attrs, int defStyleAttr)
stepsNumber = ta.getInteger(R.styleable.StepView_sv_stepsNumber, 0);
CharSequence[] descriptions = ta.getTextArray(R.styleable.StepView_sv_steps);
if (descriptions != null) {
for (CharSequence item : descriptions) {
steps.add(item.toString());
for (CharSequence description : descriptions) {
steps.add(description.toString());
}
displayMode = DISPLAY_MODE_WITH_TEXT;
} else {
Expand Down Expand Up @@ -243,9 +243,7 @@ public void setSteps(List<String> steps) {
stepsNumber = 0;
displayMode = DISPLAY_MODE_WITH_TEXT;
this.steps.clear();
if (steps != null) {
this.steps.addAll(steps);
}
this.steps.addAll(steps);
requestLayout();
go(START_STEP, false);
}
Expand Down Expand Up @@ -364,11 +362,15 @@ public int getStepCount() {

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureWidth(widthMeasureSpec);
if (getStepCount() == 0) {
setMeasuredDimension(0, 0);
setMeasuredDimension(width, 0);
return;
}
if (width == 0) {
setMeasuredDimension(width, 0);
return;
}
int width = measureWidth(widthMeasureSpec);
measureConstraints(width);
int height = measureHeight(heightMeasureSpec);
setMeasuredDimension(width, height);
Expand All @@ -388,8 +390,6 @@ private void measureConstraints(int width) {
}

private int measureHeight(int heightMeasureSpec) {
if (getMeasuredWidth() == 0) return 0;

int specSize = MeasureSpec.getSize(heightMeasureSpec);
int specMode = MeasureSpec.getMode(heightMeasureSpec);
int desiredSize = getPaddingTop()
Expand Down Expand Up @@ -437,7 +437,6 @@ private int measureStepsHeight() {
0,
true
);

int height = textLayouts[i].getHeight();
max = Math.max(height, max);
}
Expand Down Expand Up @@ -487,36 +486,20 @@ private int getMaxTextHeight() {
}

private int[] getCirclePositions() {
if (displayMode == DISPLAY_MODE_WITH_TEXT) {
return getCirclePositions(measureSteps());
} else {
return getCirclePositions(null);
}
}

private int[] measureSteps() {
int[] result = new int[steps.size()];
for (int i = 0; i < steps.size(); i++) {
result[i] = (int) StaticLayout.getDesiredWidth(steps.get(i), textPaint);
}
return result;
}

private int[] getCirclePositions(@Nullable int[] textWidth) {
int stepsCount = getStepCount();
int[] result = new int[stepsCount];

if (result.length == 0) {
return result;
}

result[0] = getStartCirclePositionRTLAware(textWidth);
result[0] = getStartCirclePosition();

if (result.length == 1) {
return result;
}

result[stepsCount - 1] = getEndCirclePositionRTLAware(textWidth);
result[stepsCount - 1] = getEndCirclePosition();

if (result.length < 3) {
return result;
Expand All @@ -538,45 +521,56 @@ private int[] getCirclePositions(@Nullable int[] textWidth) {
return result;
}

private int getStartCirclePositionRTLAware(int[] textWidth) {
int startCirclePosition;
if (isRtl()) {
startCirclePosition = getEndCirclePosition(textWidth);
private int getStartCirclePosition() {
int result;
if (displayMode == DISPLAY_MODE_WITH_TEXT) {
if (isRtl()) {
result = getMeasuredWidth() - getPaddingRight() -
Math.max(getMaxLineWidth(textLayouts[0]) / 2, selectedCircleRadius);
} else {
result = getPaddingLeft() + Math.max(getMaxLineWidth(textLayouts[0]) / 2, selectedCircleRadius);
}
} else {
startCirclePosition = getStartCirclePosition(textWidth);
if (isRtl()) {
result = getMeasuredWidth() - getPaddingRight() - selectedCircleRadius;
} else {
result = getPaddingLeft() + selectedCircleRadius;
}
}
return startCirclePosition;
return result;
}

private int getEndCirclePositionRTLAware(int[] textWidth) {
int endCirclePosition;
if (isRtl()) {
endCirclePosition = getStartCirclePosition(textWidth);
} else {
endCirclePosition = getEndCirclePosition(textWidth);
private int getMaxLineWidth(StaticLayout layout) {
int lineCount = layout.getLineCount();
int max = 0;
for (int i = 0; i < lineCount; i++) {
max = (int) Math.max(layout.getLineWidth(i), max);
}
return endCirclePosition;
return max;
}

private int getStartCirclePosition(int[] textWidth) {
private int getEndCirclePosition() {
int result;
if (displayMode == DISPLAY_MODE_WITH_TEXT) {
result = getPaddingLeft() + Math.max(textWidth[0] / 2, selectedCircleRadius);
if (isRtl()) {
result = getPaddingLeft() +
Math.max(getMaxLineWidth(last(textLayouts)) / 2, selectedCircleRadius);
} else {
result = getMeasuredWidth() - getPaddingRight() -
Math.max(getMaxLineWidth(last(textLayouts)) / 2, selectedCircleRadius);
}
} else {
result = getPaddingLeft() + selectedCircleRadius;
if (isRtl()) {
result = getPaddingLeft() + selectedCircleRadius;
} else {
result = getMeasuredWidth() - getPaddingRight() - selectedCircleRadius;
}
}
return result;
}

private int getEndCirclePosition(int[] textWidth) {
int result;
if (displayMode == DISPLAY_MODE_WITH_TEXT) {
result = getMeasuredWidth() - getPaddingRight() -
Math.max(textWidth[textWidth.length - 1] / 2, selectedCircleRadius);
} else {
result = getMeasuredWidth() - getPaddingRight() - selectedCircleRadius;
}
return result;
private <T> T last(T[] array) {
return array[array.length - 1];
}

private void measureLines() {
Expand Down

0 comments on commit 798246e

Please sign in to comment.