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

Dynamic width #21

Closed
timautin opened this issue Jan 28, 2020 · 2 comments · Fixed by #22
Closed

Dynamic width #21

timautin opened this issue Jan 28, 2020 · 2 comments · Fixed by #22

Comments

@timautin
Copy link

Hello,

Thank you for this very useful library! I quickly grabbed the code and updated it to make the view's width to be dynamic (ie it is updated when the update(...) method is called). This allows for instance to put a semi-transparent background to the MapScaleView, and to get that background's with to be updated.

Here's are the modifications I made:

Drawer.java:

    int getWidth(Scales scales) {
        return Math.round(Math.max(scales.top() == null ? 0 : scales.top().length(), scales.bottom() == null ? 0 : scales.bottom().length()) + strokePaint.getStrokeWidth());
    }

MapScaleView.java:

    private void updateScales() {
        Scale top, bottom = null;

        if (scaleType == ScaleType.MILES_ONLY) {
            top = mapScaleModel.update(false);
        } else {
            top = mapScaleModel.update(true);
            if (scaleType == ScaleType.BOTH) {
                bottom = mapScaleModel.update(false);
            }
        }

        scales = new Scales(top, bottom);
        requestLayout();
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureDimension(desiredWidth(), widthMeasureSpec);
        int height = measureDimension(desiredHeight(), heightMeasureSpec);

        mapScaleModel.setMaxWidth(width);
        drawer.setViewWidth(width);
        updateScales();

        int realWidth = drawer.getWidth(scales);

        setMeasuredDimension(realWidth, height);
    }

And here's a possible result (when I move the map, the scale bar's width is updated, including its background):

Screenshot_1580217035

Feel free to incorporate them in your code.

@pengrad pengrad linked a pull request Feb 16, 2020 that will close this issue
@pengrad
Copy link
Owner

pengrad commented Feb 16, 2020

Hi, it's definitely good improvement, thanks!

Found 2 issues in your solution - it doesn't respect fixed width (for ex width=100dp) and it breaks right-to-left layout.

I've fixed in #22 and will release with new version.

Thanks again, and feel free to open PR next time with any other improvement! :)

@timautin
Copy link
Author

Great, thanks for release a new version!
Sorry for not posting a PR, I'm a bit in a rush at the moment :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants