Android上一个优雅、万能自定义UI、仿iOS、自定义动画,支持垂直、水平方向切换、支持周视图、自定义周起始、性能高效的日历控件,支持热插拔实现的UI定制!支持标记、自定义颜色、农历、自定义月视图各种显示模式等。Canvas绘制,速度快、占用内存低,你真的想不到日历居然还可以如此优雅!An elegant, highly customized and high-performance Calendar Widget on Android.
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 是
我们也发现,如果我们把
android:layout_marginLeft
/android:layout_marginRight
从 12dp 调试到 52 dp , 这问题就不存在了。我们不明白为何会如此。如果我们还想保持 margin 12dp,请问真正解决这问题的方法是什么?谢谢。