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

Fix sizing of background drawable #1056

Open
dalewking opened this issue Jul 26, 2020 · 1 comment
Open

Fix sizing of background drawable #1056

dalewking opened this issue Jul 26, 2020 · 1 comment

Comments

@dalewking
Copy link

As described in #1052 the sizing of the drawable does not work correctly and leads to non-circular shapes. The issue is that you are trying to control the size by setting the bounds on the drawable, but that is problematic because the first time it is drawn Android sets the bounds to the size of the DayView.

I threw together this class that should accomplish what you want. Remove all the code trying to set the bounds of the drawable and instead wrap the drawable in an instance of this class:

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;

import androidx.annotation.Nullable;

public class CenterInsideDrawableWrapper extends InsetDrawable {
    private Rect tempRect = new Rect();

    public CenterInsideDrawableWrapper(@Nullable Drawable drawable) {
        super(drawable, 0);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        tempRect.set(bounds);

        final double offset = Math.abs(bounds.height() - bounds.width()) / 2.0;

        if(bounds.width() > bounds.height()) {
            tempRect.left += Math.floor(offset);
            tempRect.right -= Math.ceil(offset);
        }
        else {
            tempRect.top += Math.floor(offset);
            tempRect.bottom -= Math.ceil(offset);
        }

        // Apply inset bounds to the wrapped drawable.
        super.onBoundsChange(tempRect);
    }
}

@woocheolKim
Copy link

how to use this?

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

No branches or pull requests

2 participants