prolificinteractive / material-calendarview

A Material design back port of Android's CalendarView
https://prolificinteractive.github.io/material-calendarview/
MIT License
5.91k stars 1.32k forks source link

Fix sizing of background drawable #1056

Open dalewking opened 3 years ago

dalewking commented 3 years ago

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 commented 2 years ago

how to use this?