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

Custom Selected Day background drawable is becoming oval #1052

Open anallur opened 4 years ago

anallur commented 4 years ago

We wanted to reduce the size of the Selected day background circle. We used a custom drawable to put as the selection drawable in a new Decorator. We have a custom tile height, which makes the day view as a rectangle than a square.

circle_drawable ?xml version="1.0" encoding="utf-8"?> inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetTop="5dp" android:insetLeft="13dp" android:insetRight="13dp" android:insetBottom="5dp"> shape android:shape="oval"> solid android:color="@color/colorPrimary" /> size android:width="@dimen/dimen_12" android:height="@dimen/dimen_12" /> /shape> /inset>

inset tag close

Can you please help, on how we can get a circle background drawable always?

Screenshot 2020-07-09 at 10 23 07 AM Screenshot 2020-07-09 at 10 20 41 AM
anallur commented 4 years ago

I am not able to post the correct circle_drawable code, i have left out < infront of the tags on purpose

dalewking commented 4 years ago

We are experiencing the same thing with out using a custom drawable. For some reason when the calendar first appears the circle starts as an oval then switches to a circle after a fraction of a second. So you see a flash of the oval, which is enough to make it really look unprofessional.

dalewking commented 4 years ago

So after some debugging I think I see what is going on. In DayView.onLayout:

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    calculateBounds(right - left, bottom - top);
    regenerateBackground();
  }

Here you calculate the bounds which sets circleDrawableRect for the standard circle background and tempRect with the bounds for a custom drawable. Then in regenerateBackground you actually build the circle drawable or get the custom drawable and set it as the backgroundDrawable for the DayView, BUT you did not set the bounds on this drawable so it has no bounds.

You don't actually set the bounds for the background drawable until onDraw():

  @Override
  protected void onDraw(@NonNull Canvas canvas) {
    if (customBackground != null) {
      customBackground.setBounds(tempRect);
      customBackground.setState(getDrawableState());
      customBackground.draw(canvas);
    }

    mCircleDrawable.setBounds(circleDrawableRect);

    super.onDraw(canvas);
  }

The problem is that in the View.draw code the backgroundDrawable is drawn before onDraw is called. The drawing of the backgroundDrawable will resize it to the size of the DayView because no bounds have been set. After it gets drawn at the size of the DayView you then change the bounds in onDraw and it will get redrawn the correct size.

dalewking commented 4 years ago

After playing with it some more the real issue is that trying to control the bounds of the background drawable is not the right way to resize it because the first time it is drawn its bounds will get set to the size of the view. What you really need to do instead of adjusting the bounds of the drawable is wrap the drawable in an InsetDrawable.