Why custom controls?

1. Specific display style
2. Handle unique user interactions

Example: For example, the original TextView can not slide the text inside, through the custom control to achieve

3. Optimize our layout

For example: To achieve complex layout through nesting, but the efficiency of drawing and measuring is slow. To improve efficiency, customize the control

4. Packaging, etc

For example: Many controls in the APP can be reused, such as the TAB button at the bottom of the home page, which is packaged as a custom control for easy subsequent use

How do I customize controls?

1. Declaration and acquisition of custom attributes

Extract the properties of the custom control, declare them, and get them in the constructor.

  • Analyze required custom attributes: color, text size, text, icon, etc
  • Define the declaration in res/values/attrs.xml
  • Use it in layout.xml
  • In the View of the construction method to obtain through TypedArray a = context. ObtainStyledAttributes () to obtain, return to a TypedArray object, through this object can be obtained to the corresponding custom attribute’s value, Recycle (); Intermediate fetching can be done using the for loop or directly fetching
2. Measure the onMeasure

How much range is needed to measure itself, determined by two numbers, one mode of measurement, one value of measurement there are three modes of measurement:

  • EXACTLY: If the exact value is set, then result equals our exact value
  • AT_MOST: cannot exceed a certain value at most. This mode usually occurs when wrap_content is set. The size is determined by the content, but cannot exceed the height and width of the parent control
  • UNSPECIFIED: MeasureSpec = MeasureSpec = MeasureSpec = MeasureSpec = MeasureSpec = MeasureSpec = MeasureSpec Finally, remember to use the setMeasuredDimension method to pass in the resulting result value. If remeasurement is required, requestLayout() is used to provide external remeasurement, excluding drawing
3. OnLayout (ViewGroup)

A pure View doesn’t need to worry about that, a View group needs to copy this method

  • Determines the position of the child View
  • Move as many operations from onMeasure to this method as possible (time-consuming operations move here, onLayout fires only once)
  • Calculate the location and use the requestLayout() method to create the layout
4. Draw onDrow
  • Draw the Content area: Use the Canvas-related API to draw the effect you want
  • Invalidate (call from UI line city), postInvalidate (call from child thread)
  • Cancas.drawxxx proficiently uses some of these methods
  • Translate, rotate, scale, skew skillfully use these methods
  • In general, if there is no interaction with the user, only onMeasure and onDrow can be considered theoretically. In case of a custom ViewGroup, onLayout needs to be considered
5.onTouchEvent

If there is any interaction, it needs to be handled here

  • ACTION_DOWN: Lower the finger
  • ACTION_MOVE: Move a finger
  • ACTION_UP: Lift the finger, if there is added to speed detection, etc., judge here
  • ACTION_POINTER_DOWN: Indicates the type to be considered when considering multi-touch
  • ACTION_POINTER_UP: Type to be considered when considering multi-touch
  • GetParent (). RequestDisallowInterceptTouchEvent (true) if the view had already got hold of the event, you can call this method, you can tell the parent controls, don’t block me
  • VelocityTracker
6. OnInterceptTouchEvent (ViewGroup)

In the event forwarding process, the event is handled by the child control, but in the forwarding process, the parent control has the right to intercept the child control’s event, that is, through this method, if the return true, it indicates that the event was blocked, the event is handed to the ViewGroup to handle

  • ACTION_DOWN: Lower the finger
  • ACTION_MOVE: Move the finger to calculate the moving distance
  • ACTION_UP: Lift the finger, if there is added to speed detection, etc., judge here
  • ACTION_POINTER_DOWN: Indicates the type to be considered when considering multi-touch
  • ACTION_POINTER_UP: Type to be considered when considering multi-touch
  • GetParent (). RequestDisallowInterceptTouchEvent (true) if the view had already got hold of the event, you can call this method, you can tell the parent controls, Don’t block my event decide whether to block that gesture or not if you’re customizing a ViewGroup and you want to block a method in a child View, you also need to write this method.