huanghaibin-dev / CalendarView

Android上一个优雅、万能自定义UI、仿iOS、自定义动画,支持垂直、水平方向切换、支持周视图、自定义周起始、性能高效的日历控件,支持热插拔实现的UI定制!支持标记、自定义颜色、农历、自定义月视图各种显示模式等。Canvas绘制,速度快、占用内存低,你真的想不到日历居然还可以如此优雅!An elegant, highly customized and high-performance Calendar Widget on Android.
Apache License 2.0
9.16k stars 1.8k forks source link

在边缘进行的自行绘画会不完整 #219

Closed yccheok closed 6 years ago

yccheok commented 6 years ago

嗨,我们发现,如果自行绘画是在边缘进行时,会不完整。如下

untitled

我们的代码是

public class CustomMonthView extends MonthView {
    ...

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        // fill circle with 1 of Note's color
        int fillColor = calendar.getSchemeColor();

        circleFillPaint.setColor(fillColor);

        float xOrigin = x + mItemWidth - padding - circleRadius / 2f;
        float yOrigin = y + padding + circleRadius;

        canvas.drawCircle(
                xOrigin,
                yOrigin,
                circleRadius,
                circleFillPaint);

        // for white color, draw a grey color border, 2dp thickness
        if (fillColor == whiteNoteColor) {
            circleBorderPaint.setColor(circleBorderColor);

            canvas.drawCircle(
                    xOrigin,
                    yOrigin,
                    circleBorderRadius,
                    circleBorderPaint
            );
        }

        // only show 1 digit, if number of note > 9, show nothing. because more digits might not fit into circle
        String scheme = calendar.getScheme();
        if (scheme.length() > 1) {
            return;
        }

        final Paint textPaint;
        final float yOffset;

        if (fillColor == whiteNoteColor || fillColor == yellowNoteColor) {
            // for white, yellow color, use black color text
            textPaint = this.textInversePaint;
            yOffset = textInverseBaseLine;
        } else {
            // white color text
            textPaint = this.textPaint;
            yOffset = textBaseLine;
        }

        String text = calendar.getScheme();
        float xOffset = textPaint.measureText(text) / 2.0f;

        canvas.drawText(
                text,
                xOrigin - xOffset,
                yOrigin - yOffset,
                textPaint);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        //int top = y - mItemHeight / 12;
        int top = y;

        final Paint textPaint;
        if (isSelected) {
            textPaint = mSelectTextPaint;
        } else if (calendar.isCurrentDay()) {
            textPaint = mCurDayTextPaint;
        } else if (calendar.isCurrentMonth()) {
            textPaint = mCurMonthTextPaint;
        } else {
            textPaint = mOtherMonthTextPaint;
        }

        canvas.drawText(
                String.valueOf(calendar.getDay()),
                cx,
                mTextBaseLine + top,
                textPaint);
    }
}

XML 是

    <com.haibin.calendarview.CalendarLayout
        android:id="@+id/calendar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/calendarBackgroundColor"
        android:orientation="vertical"
        app:calendar_show_mode="only_month_view"
        app:default_status="expand">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.haibin.calendarview.CalendarView
                android:id="@+id/calendar_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/calendarBackgroundColor"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"

                app:month_view="com.yocto.wenote.calendar.CustomMonthView"
                app:month_view_show_mode="mode_fix"

                app:current_month_text_color="?attr/calendarCurrentMonthTextColor"
                app:other_month_text_color="?attr/calendarOtherMonthTextColor"

                app:day_text_size="16sp"
                app:current_day_text_color="?attr/calendarCurrentDayTextColor"
                app:selected_text_color="?attr/calendarSelectedTextColor"
                app:selected_theme_color="?attr/calendarSelectedThemeColor"

                app:week_bar_view="com.yocto.wenote.calendar.CustomWeekBar"
                app:week_background="?attr/calendarBackgroundColor"
                app:week_line_background="?attr/calendarWeekLineBackgroundColor"
                app:week_bar_height="46dp"
                app:week_start_with="sun"

                app:month_view_scrollable="false"
                app:week_view_scrollable="false"
                app:year_view_scrollable="false"

                app:year_view_background="?attr/calendarYearBackgroundColor"
                app:year_view_day_text_color="?attr/primaryTextColor"
                app:year_view_day_text_size="9sp"
                app:year_view_month_text_color="?attr/calendarYearMonthTextColor"
                app:year_view_month_text_size="20sp"
                app:year_view_scheme_color="?attr/calendarYearSchemeColor" />

        </ScrollView>

    </com.haibin.calendarview.CalendarLayout>

我们也发现,如果我们把 android:layout_marginLeft/android:layout_marginRight 从 12dp 调试到 52 dp , 这问题就不存在了。

111

我们不明白为何会如此。如果我们还想保持 margin 12dp,请问真正解决这问题的方法是什么?谢谢。

huanghaibin-dev commented 6 years ago

没什么问题的,关键在于绘制的控制,当然不建议直接使用margin,使用calendar_padding attr即可

yccheok commented 6 years ago

谢谢。我们用 app:calendar_padding="12dp" 解决了。