Android third-party library CalendarView
According to the needs and the way the library is used, I made a calendar that suits me, and only recorded it so that I can make other styles of calendars next time. address
need:
-
Only show data for the current month
-
The default month view has rectangular lines
-
The selected days also have a selected rectangle
-
Today's item needs to display "today"
-
Part of the record to display pictures
1. Layout code
<com.haibin.calendarview.CalendarView android:id="@+id/calendarView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" app:calendar_height="46dp" app:calendar_padding="10dp" app:current_month_lunar_text_color="#CFCFCF" app:current_month_text_color="#333333" app:min_year="2004" app:month_view="com.haibin.calendarviewproject.zhengq.SimpleMonthView" app:month_view_show_mode="mode_only_current" app:other_month_text_color="#e1e1e1" app:scheme_text="假" app:scheme_text_color="#333" app:scheme_theme_color="#333" app:selected_text_color="#333" app:selected_theme_color="#333" app:week_background="#fff" app:week_text_color="#111" app:week_view="com.haibin.calendarviewproject.zhengq.SimpleWeekView" app:year_view_day_text_color="#333333" app:year_view_day_text_size="9sp" app:year_view_month_text_color="#ff0000" app:year_view_month_text_size="20sp" app:year_view_scheme_color="#f17706" />
The month_view_show_mode attribute can be set to only display the current month's data
The calendar_height attribute can set the height of the month item
2. The default rectangular frame
@Override protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) { // Draw text here, don't ask me how to hide the lunar calendar, don't ask me how to change a certain date It has become a special string. You can draw here how you want to display it. If you don’t draw it, it will not be displayed. It depends on how you want to display the calendar, not the frame. canvas.drawRect(x, y, x + mItemWidth, y + mItemHeight, mRectPaint); ... }
3. The selected rectangle
@Override protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) { // Draw the selected day style here, see if you need to continue calling onDrawScheme //Draw the selected date rectangle mSelectedPaint.setColor( getContext().getResources().getColor(R.color.solar_background)); mSelectedPaint.setStyle(Paint.Style.STROKE); canvas.drawRect(x, y, x + mItemWidth, y + mItemHeight, mSelectedPaint); return true;//If this is false, select an item with Scheme, do not display Scheme, set true and it will be displayed }
4.scheme
/** * Draw marked event days * * @param canvas canvas * @param calendar calendar calendar * @param x calendar Card x start coordinates * @param y calendar Card y start coordinates */ @Override protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) { //Draw the marked date style here, you can do whatever you want // Draw to the left of the date number canvas.drawText(calendar.getScheme(), x + mPadding, y + mItemHeight / 2 + mPadding, mTextPaint); // Draw a picture Drawable drawable = getContext().getResources().getDrawable(R.mipmap.record_gong_l); //First layer first canvas.drawBitmap(BitmapUtils. drawable2Bitmap(drawable), x + mPadding, y + mPadding, mTextPaint); //First layer first two //The second canvas.drawBitmap of the (BitmapUtils.drawable2Bitmap(drawable), x + mItemWidth / 2 - drawable.getIntrinsicWidth() / 2, y + mPadding, mTextPaint); //The third canvas of the first layer. drawBitmap(BitmapUtils.drawable2Bitmap(drawable), x + mItemWidth - drawable.getIntrinsicWidth() - mPadding, y + mPadding, mTextPaint); // The third canvas.drawBitmap(BitmapUtils.drawable2Bitmap(drawable), x + mItemWidth - drawable.getIntrinsicWidth() - mPadding, y + mItemHeight / 2 - drawable.getIntrinsicHeight() / 2, mTextPaint); // The first canvas.drawBitmap of the third layer(BitmapUtils.drawable2Bitmap(drawable), x + mPadding , - mPadding + mItemHeight - drawable.getIntrinsicHeight(), mTextPaint); //The second layer of the third layer indivual canvas.drawBitmap(BitmapUtils.drawable2Bitmap(drawable), x + mItemWidth / 2 - drawable.getIntrinsicWidth() / 2, y - mPadding + mItemHeight - drawable.getIntrinsicHeight(), mTextPaint); //第三层第三个 canvas.drawBitmap(BitmapUtils.drawable2Bitmap(drawable), x + mItemWidth - drawable.getIntrinsicWidth() - mPadding, y - mPadding + mItemHeight - drawable.getIntrinsicHeight(), mTextPaint); }
5. finish
6. Pay attention
1.Paint color settings
public SimpleMonthView(Context context) { super(context); mRectPaint.setStyle(Paint.Style.STROKE); mRectPaint.setStrokeWidth(dipToPx(context, 0.5f)); mRectPaint.setColor(0x88efefef);//The demonstration here cannot be set A color with a transparency of 1 will cover the selected color mPadding = dipToPx(getContext(), 4); mTextPaint.setTextSize(dipToPx(context, 8)); mTextPaint.setTextSize(dipToPx(context, 10)); mTextPaint.setColor (getContext().getResources().getColor(R.color.solar_background));//black mTextPaint.setAntiAlias(true); mTextPaint.setFakeBoldText(true); }
2. SimpleWeekView and SimpleMonthView should be the same
3. return true in onDrawSelected; otherwise the scheme will not be displayed
4. Disadvantages: A white border will be displayed next to the calendar