Implementation of Android custom view calendar interface example

Now there are many custom views on the Internet to implement the demo of calendar. Today, let's talk about how to implement this custom view.

Take a look at the final rendering:

In this custom view, I used various ingenious methods to implement this calendar. It's really hard. Cut the crap and start going into the pit.

Interface analysis

The head is a textview that displays the year and month, and then the next line is the day of the week. These two lines can be fixed and do not go in and out of the screen with month switching.

Next is the protagonist of our custom view, the number of days per month. At present, Sunday is the first day of every week. The first line is filled with the number of days of the previous month, and the last line is filled with the first few days of the next month. The color is set to gray. Monday to Friday in the date of the current month is set to red, Saturday and Sunday are set to cyan, and the special date is set to green. The special identifier is filled in the upper right corner and wrapped with three-quarters of the arc (there is no date of the previous month and the next month).

Here is another small detail. The total number of total rows per month will continue to change, but the total height of the view has not changed, so the visual effect will be different.

Construction method

It mainly implements the above two construction methods. The first is used in Java code and the second is used in XML layout file.

Exposed interface

At present, there are the following interfaces: setDate (customdate, customdate), setweekendhighlight (Boolean b), setspecialday (int [] ints)

The first one must be set, otherwise nothing will be displayed. The second one is whether to highlight the weekend, the third one is the specially displayed date, and the fourth one is whether to click the date of the previous month or the next month. It is not set by default. Other interfaces can be added later according to your needs.

Here is a description of the method of calculating the number of display lines. First of all, we should pay attention to the problem that there will be an increase of one day between the week we get and the actual day of the week, that is, the current is week 4, so the int you get will be 5

After subtracting one from the first day of the week and adding the total number of days of the month, we can get where the last day is, then divide it by seven to get the integer part of the quotient, and then we can get how many rows should be displayed in the forward method.

Onsizechanged method

The displayed size can be obtained in the onsizechanged method. At this time, we need to do some work:

First, the width and height are introduced to facilitate later use.

The cutgrid () method divides the region into rows and X columns.

The init () method initializes some brushes.

The setcellday () method maps the days of each month to the coordinates.

Let's first look at the cutgrid () method:

Cellwidth is the width of each day, where row_ Count is a constant 7, representing 7 days a week; Cellheight is the height of each line, and linecount is a variable, which needs to be calculated according to the date, as will be mentioned later; Radius is the radius of the area we draw. This value is half the smaller value of width and height. Then we put each grid center coordinate point into a list < point > points using a double loop.

The whole view is divided into the above shape.

Let's take a look at the init () method:

Basically brush tools.

Then comes the setalldays () method:

Here we use a custom class cellday.

Cellday has the following fields

1. String date indicates the current date.

2. Daystate is an American drama type that defines the state value of days.

Multiple states can be set, and the usage is basically the same as that of special day.

Customdate tool

The description of each method in the comments is very clear.

The number of days of the previous month is used to calculate the day of the week on the last day of the previous month, and then deduce the number of days displayed in the current month of the previous month and the corresponding week.

Get the day of the week on the first day of the month, and then sort the days of the month and the corresponding week.

Get the total number of days this month. The first day is the day of the week, which is used to calculate the total number of lines, that is, to determine the linenumber.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>