custom control

1. Declare properties

attrs.xml;

2. Measurement

MeasureSpec : 30 bits, the first two bits are the mode, and the last 28 bits are the specific size value.

mode = MeasureSpce.getMode(heightMeasureSpec);

size = MeasureSpec.getSize(heightMeasureSpec);

if(mode = MeasureSpec.EXACTLY){

result = size;

}

onMeasure : measure the size of itself

three modes 

EXACTLY Completely based on its own value, such as 100dp , used for match_parent , at this time result=size

AT_MOST Maximum size cannot exceed a value for warp_content

INSPECIFIED no size limit, used for listview , scrollview

setMeasureDimesions() : Pass in the result of the measurement and set the size

requestLayout() : request remeasurement in the parent control

3. Layout:

onLayout() : In the viewgroup , the parent control sets the position where the child view is displayed

4. Draw

onDraw() : draw the content area;

The difference between invalidate() and postInvalidate() : Called in UI thread and non- UI thread;

5. Interaction

onTouchEvent() : used when the custom control interacts with the user

When there is acceleration detection, initialize initVelocityTrackerIfNotExsits

6. Intercept

onInterceptTouchEvent() : The method in the viewgroup , the parent control intercepts the event and handles it by itself, preventing it from being passed to the child control


Custom view process:

Analyze the required attributes of the view , write it in the xml file, and obtain, measure, layout, and draw in the construction method in the view ;


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325526639&siteId=291194637