TimePicker source code learning-TimePicker structure and data structure source code learning

Source: Internet
Author: User

TimePicker source code learning-TimePicker structure and data structure source code learning

Although I have read a lot of Android source code and the source code analysis written by others, however, I always feel that the written content does not fully display the code written by the Code author or the thinking process of reading the source code by myself. (It cannot be described in words)

This article summarizes the commonly used time selector TimePicker source code. I may not analyze the source code step by step from the beginning. It is just a summary after reading it, of course, it is only after learning and applying it.

Spinner:

 

 

I. From TimePicker. java 1. timePicker inherits FrameLayout, indicating that TimePicker is only used for Layout rather than for specific painting. The specific drawing view must be painted in another place, and then arranged on this FrameLayout (this is critical! Why ?)
public class TimePicker extends FrameLayout{    //......       }

2. Start with the TimePicker constructor.

Here we can see MODE_CLOCK and MODE_SPINNER, which are two formats of TimePicker (you can set clockt or spinner for the timePickerMode attribute in the layout,This article targets api23 and finds that android4.0 only has one mode.), One corresponds to TimePickerClockDelegate, And the other corresponds to TimePickerSpinnerDelegate. Note that this is TimePicker, and TimePicker is passed into the mDelegate constructor. You can see that the timer is finally drawn in mDelegate and laid out in mDelegate.

public TimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {    super(context, attrs, defStyleAttr, defStyleRes);    final TypedArray a = context.obtainStyledAttributes(            attrs, R.styleable.TimePicker, defStyleAttr, defStyleRes);    final int mode = a.getInt(R.styleable.TimePicker_timePickerMode, MODE_SPINNER);    a.recycle();    switch (mode) {        case MODE_CLOCK:            mDelegate = new TimePickerClockDelegate(                    this, context, attrs, defStyleAttr, defStyleRes);            break;        case MODE_SPINNER:        default:            mDelegate = new TimePickerSpinnerDelegate(                    this, context, attrs, defStyleAttr, defStyleRes);            break;    }}

Next we will move to TimePickerClockDelegate, because TimePickerSpinnerDelegate does not have to analyze both of them.

Ii. Check TimePickerClockDelegate. java again,

1. It also starts from the constructor,

Use an inflate code in the constructor to expand the layout file. We can see that the layout content is expanded into the delegator, And this is TimePicker !! Now we know why TimePicker inherits FrameLayout. From the above we can see that Timepicker has been passed in when it is constructed.

// The following is TimePickerClockDelegate. java code: final int layoutResourceId =. getResourceId (R. styleable. timePicker_internalLayout, R. layout. time_picker_material); final View mainView = inflater. inflate (layoutResourceId, delegator); mHeaderView = mainView. findViewById (R. id. time_header );

In addition, we can compare the code TimePickerClockDelegate and TimePickerSpinnerDelegate, and find that the style is a bit different. It may be written by two or the same person at different times,

// The following is TimePickerSpinnerDelegate. java: final LayoutInflater inflater = LayoutInflater. from (mContext); inflater. inflate (layoutResourceId, mDelegator, true); // hourmHourSpinner = (NumberPicker) delegator. findViewById (R. id. hour );

2. I will summarize the member variables of TimePickerClockDelegate and form the class graph structure of TimePicker.

You can see the relationship between classes used by TimePicker.

3. TimePickerClockDelegate is a combined custom view.

From the TimePickerClockDelegate code, you can see that there is no onDraw method in it, indicating that it is a combined custom view.

In the class diagram, we can see that the dial is drawn in the RadialTimePickerView from its member variables and the clock display mode in timepicker.

RadialTimePickerVIew: the dial in the clock-type time selector. Other textviews: the number of hours and minutes displayed, and the last afternoon. (You can see the member variables of TimePickerCLockDelegate)

 

4. the last and most important thing is the drawing of the dial RadialTimePickerVIew. The knowledge or thoughts in the dial can be used in many places. For example, a text color or background color (in fact, it uses the knowledge of the complementary set and the remainder set in the drawing area ).

 

 

Summary: This article mainly describes the code structure of the TimePicker class in Android. The Code details are not too detailed and you can learn a lot from them.

1) For example, if a custom control is complicated, you can break it down and combine it into a whole.

2) in order to expand or be compatible, the proxy mode can be used for specific implementation.

 

Related Article

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.