Masturbating an Android high-performance calendar control, high-imitation Meizu

Source: Internet
Author: User
Tags drawtext getcolor

Android native CalendarView can not meet the needs of our daily development, in the process of developing my mind app, I feel the need to come up with a high-performance and beautiful simple Calendar control, feel Meizu's calendar style is very suitable, so intend to make a new one.

GitHub Address: Https://github.com/huanghaibin-dev/CalendarView

Compile ' com.haibin:calendarview:1.0.2 '

First on:

Before we start, we need to analyze how Meizu designs such a high-performance calendar, and we open the display layout boundary in the developer options:

Well, at first I thought the Calendar interface was Viewpager+recyclerview, but it's obviously not, if it's recyclerview, then we assume that every month's card has 5*7=35 item, Each item root layout is a relativelayout+3 textview, and we approximate the controls to be loaded when the calendar is initialized:

3 Viewpager Item * 35 Recyclerview Item * 4 (Number of controls per item) + 8 (week bar) = 420+

Oh, my God, this can't be done, obviously performance is compromised, let's take a look at the month control:

Well, this looks like Viewpager+recyclerview to do, each Recyclerview item is just a control, inside the text is drawn, here is probably the analysis of clear.

We take a compromise approach, the calendar interface and the month card interface are all in Viewpager+recyclerview way, the difference is that all the item we use custom Viewcanvas to draw the way to do, so performance, although not inferior to Meizu, but the speed of the experience basically similar, Let's look at the item code of the calendar interface: just draw 3 text

public class Cellview extends View {private int mday = 20;    Private String Mlunar;    Private String Mscheme;    Private Paint Mdaypaint = new paint ();    Private Paint Mlunarpaint = new paint ();    Private Paint Mschemepaint = new paint ();    Private Paint Mcirclepaint = new paint ();    private int Mradius;    private int mcirclepadding;    private int mcirclecolor;    Public Cellview (Context context) {This (context, NULL);        } public Cellview (context context, @Nullable AttributeSet attrs) {Super (context, attrs);        Mdaypaint.setantialias (TRUE);        Mdaypaint.setcolor (Color.Black);        Mdaypaint.setfakeboldtext (TRUE);        Mdaypaint.settextalign (Paint.Align.CENTER);        Mlunarpaint.setantialias (TRUE);        Mlunarpaint.setcolor (Color.gray);        Mlunarpaint.settextalign (Paint.Align.CENTER);        Mschemepaint.setantialias (TRUE);        Mschemepaint.setcolor (Color.White);        Mschemepaint.setfakeboldtext (TRUE); Mschemepaint.settextaLign (Paint.Align.CENTER);        Mcirclepaint.setantialias (TRUE);        Mcirclepaint.setstyle (Paint.Style.FILL);        TypedArray array = context.obtainstyledattributes (Attrs, R.styleable.cellview);        Mdaypaint.settextsize (Array.getdimensionpixelsize (r.styleable.cellview_cell_day_text_size, 18));        Mlunarpaint.settextsize (Array.getdimensionpixelsize (R.styleable.cellview_cell_lunar_text_size, 12));        Mradius = (int) array.getdimension (R.styleable.cellview_cell_scheme_radius, 8);        Mschemepaint.settextsize (Array.getdimensionpixelsize (r.styleable.cellview_cell_scheme_text_size, 6));        mcirclepadding = Array.getdimensionpixelsize (r.styleable.cellview_cell_circle_padding, 4);        Mcirclepaint.setcolor (Array.getcolor (R.styleable.cellview_cell_circle_color, 0xff16bb7f));    Array.recycle ();        } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas);        int width = getwidth ();        int height = getheight (); int w = (WIDth-getpaddingleft ()-getpaddingright ());        int h = (Height-getpaddingtop ()-Getpaddingbottom ())/4;        Canvas.drawtext (String.valueof (Mday), W/2, 2 * H + getpaddingtop (), mdaypaint);        Canvas.drawtext (Mlunar, W/2, 4 * H + getpaddingtop (), mlunarpaint); if (! Textutils.isempty (Mscheme)) {canvas.drawcircle (W/2 + mcirclepadding + mdaypaint.gettextsize (), GetPaddingTop            () + H, Mradius, Mcirclepaint); Canvas.drawtext (Mscheme, W/2 + mcirclepadding + mdaypaint.gettextsize (), getpaddingtop () + MRADIUS/2 + H, mSchemePaint        ); }}/** * Initialize Calendar * @param day * @param lunar lunar * @param scheme event flag */void init (int day, S        Tring Lunar, String scheme) {this.mday = day;        This.mlunar = lunar;    This.mscheme = scheme;        } void SetTextColor (int textcolor) {mdaypaint.setcolor (textcolor);    Mlunarpaint.setcolor (TextColor); } void Setcirclecolor (int circlecolor) {MCiRclepaint.setcolor (Circlecolor);    Invalidate (); }}

Month Card Custom View

public class MonthView extends View {private int mdiff;//first day deviation Sunday how many days private int mcount;//total private int Mlastcou    nt;//the number of days of the last line private int mline;//How many lines private paint mpaint = new paint ();    Private Paint Mschemepaint = new paint ();    Private list<calendar> mschemes;    Private Calendar Mcalendar;    Public MonthView (Context context) {This (context, NULL);        } public MonthView (context context, @Nullable AttributeSet attrs) {Super (context, attrs);        Mpaint.setantialias (TRUE);        Mpaint.settextalign (Paint.Align.CENTER);        Mschemepaint.setantialias (TRUE);        Mschemepaint.settextalign (Paint.Align.CENTER);        TypedArray array = context.obtainstyledattributes (Attrs, R.styleable.monthview);        Mpaint.settextsize (Array.getdimensionpixelsize (R.styleable.monthview_month_view_text_size, 12));        Mschemepaint.settextsize (Array.getdimensionpixelsize (R.styleable.monthview_month_view_text_size, 12)); Mpaint.setcolor (Array.getColor (R.styleable.monthview_month_view_text_color, Color.Black));        Mschemepaint.setcolor (Array.getcolor (R.styleable.monthview_month_view_remark_color, Color.RED));        Array.recycle ();    Measureline ();        } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas);        int width = getwidth ();        int height = getheight ();        int pleft = Getpaddingleft ();        int w = (Width-getpaddingleft ()-getpaddingright ())/7;        int h = (Height-getpaddingtop ()-Getpaddingbottom ())/6;        int d = 0;  for (int i = 0; i < mline; i++) {if (i = = 0) {///First line for (int j = 0; J < (7-mdiff), J + +)                    {++d;                Canvas.drawtext (String.valueof (j + 1), Mdiff * W + J * W + pleft + w/2, H, Isscheme (d)? mschemepaint:mpaint); }} else if (i = = MLine-1 && Mlastcount! = 0) {int first = Mcount-mlastcou                NT + 1;for (int j = 0; J < Mlastcount; J + +) {++d;                    Canvas.drawtext (string.valueof (first), J * W + pleft + W/2, (i + 1) * H, Isscheme (d)? mschemepaint:mpaint);                ++first;                }} else {int first = i * 7-mdiff + 1;                    for (int j = 0; J < 7; J + +) {++d;                    Canvas.drawtext (string.valueof (first), J * W + pleft + W/2, (i + 1) * H, Isscheme (d)? mschemepaint:mpaint);                ++first; }}}}/** * Count rows */private void Measureline () {int offset = mCount-(7-MD        IFF);        Mline = 1 + (offset% 7 = = 0? 0:1) + Offset/7;    Mlastcount = offset% 7;     }/** * Initialize month card * @param mdiff deviation days * @param mCount Month total number of days * @param myear Year * @param mmonth which month        */void init (int mdiff, int mCount, int myear, int mmonth) {This.mdiff = Mdiff;This.mcount = MCount;        Mcalendar = new Calendar ();        Mcalendar.setyear (myear);        Mcalendar.setmonth (Mmonth);        Measureline ();    Invalidate ();    } void Setschemes (List<calendar> mschemes) {this.mschemes = Mschemes;        } void Setschemecolor (int schemecolor) {if (SchemeColor! = 0) Mschemepaint.setcolor (schemecolor);    if (SchemeColor = = 0xff30393e) mschemepaint.setcolor (color.red);        } private Boolean isscheme (int day) {if (Mschemes = = NULL | | mschemes.size () = = 0) return false;        Mcalendar.setday (day);    Return Mschemes.contains (Mcalendar); }}

  

Other code is not difficult, calendar algorithm is found on GitHub, more details please see Warehouse address: Https://github.com/huanghaibin-dev/CalendarView

Masturbating an Android high-performance calendar control, high-imitation Meizu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.