Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to config max width of scalebar. #1048

Merged
merged 3 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,25 @@ public void testMetrics() {
assertFalse(scaleBarWidget.isMetricUnit());
});
}

@Test
public void testRatio() {
validateTestSetup();
setupScaleBar();
assertEquals(0.5f,scaleBarWidget.getRatio(), 0f);
invoke(mapboxMap, (uiController, mapboxMap) -> {
ScaleBarOptions option = new ScaleBarOptions(activity);
option.setMaxWidthRatio(0.1f);
scaleBarWidget = scaleBarPlugin.create(option);
assertNotNull(scaleBarWidget);
assertEquals(0.1f, scaleBarWidget.getRatio(), 0f);
});
invoke(mapboxMap, (uiController, mapboxMap) -> {
ScaleBarOptions option = new ScaleBarOptions(activity);
option.setMaxWidthRatio(1.0f);
scaleBarWidget = scaleBarPlugin.create(option);
assertNotNull(scaleBarWidget);
assertEquals(1.0f, scaleBarWidget.getRatio(), 0f);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ScalebarActivity : AppCompatActivity() {
.setMarginTop(15f)
.setMarginLeft(16f)
.setTextBarMargin(15f)
.setMaxWidthRatio(0.5f)

scaleBarPlugin.create(scaleBarOptions)
fabScaleWidget.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.ContextCompat;
Expand All @@ -28,6 +29,7 @@ public class ScaleBarOptions {
private float borderWidth;
private float textSize;
private boolean isMetricUnit;
private float ratio;

public ScaleBarOptions(@NonNull Context context) {
this.context = context;
Expand All @@ -42,6 +44,7 @@ public ScaleBarOptions(@NonNull Context context) {
setTextColor(android.R.color.black);
setPrimaryColor(android.R.color.black);
setSecondaryColor(android.R.color.white);
setMaxWidthRatio(0.5f);
}

/**
Expand All @@ -62,6 +65,7 @@ ScaleBarWidget build() {
scaleBarWidget.setSecondaryColor(secondaryColor);
scaleBarWidget.setTextColor(textColor);
scaleBarWidget.setTextSize(textSize);
scaleBarWidget.setRatio(ratio);
return scaleBarWidget;
}

Expand Down Expand Up @@ -256,6 +260,16 @@ public ScaleBarOptions setTextBarMargin(@DimenRes int textBarMargin) {
return this;
}

/**
* Set the ratio of scale bar max width compared with MapView width.
*
* @param ratio the ratio scale bar will use, must be in the range from 0.1f to 1.0f.
*/
public ScaleBarOptions setMaxWidthRatio(@FloatRange(from = 0.1f, to = 1.0f) float ratio) {
this.ratio = ratio;
return this;
}

/**
* Helper class to determine the user measuring system.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ScaleBarWidget extends View {
private float textSize;
private double distancePerPixel;
private boolean isMetricUnit;
private float ratio;
private ArrayList<Pair<Integer, Integer>> scaleTable;
private String unit;
private final RefreshHandler refreshHandler;
Expand All @@ -59,7 +60,7 @@ protected void onDraw(Canvas canvas) {
if (distancePerPixel <= 0) {
return;
}
double maxDistance = mapViewWidth * distancePerPixel / 2;
double maxDistance = mapViewWidth * distancePerPixel * ratio;
Pair<Integer, Integer> pair = scaleTable.get(0);
for (int i = 1; i < scaleTable.size(); i++) {
pair = scaleTable.get(i);
Expand All @@ -71,7 +72,7 @@ protected void onDraw(Canvas canvas) {
}

int unitDistance = pair.first / pair.second;
float unitBarWidth = maxBarWidth / 2f;
float unitBarWidth = maxBarWidth / pair.second;
if (unitDistance == 0) {
unitDistance = 1;
} else {
Expand Down Expand Up @@ -179,7 +180,7 @@ public float getMarginLeft() {
*/
public void setMarginLeft(float marginLeft) {
this.marginLeft = marginLeft;
maxBarWidth = mapViewWidth / 2f - marginLeft;
maxBarWidth = mapViewWidth * ratio - marginLeft;
}

/**
Expand Down Expand Up @@ -346,7 +347,7 @@ public int getMapViewWidth() {
*/
void setMapViewWidth(int mapViewWidth) {
this.mapViewWidth = mapViewWidth;
maxBarWidth = mapViewWidth / 2f - marginLeft;
maxBarWidth = mapViewWidth * ratio - marginLeft;
}

/**
Expand All @@ -364,6 +365,22 @@ private String getDistanceText(int distance) {
}
}

/**
* Set the ratio of scale bar max width compared with MapView width.
* @param ratio the ratio scale bar will use.
*/
public void setRatio(float ratio) {
Chaoba marked this conversation as resolved.
Show resolved Hide resolved
this.ratio = ratio;
}

/**
* Get the current ratio of scale bar.
* @return current ratio.
*/
public float getRatio() {
return this.ratio;
}

/**
* Handler class to limit the refresh frequent.
*/
Expand Down