Android control architecture and custom controls

Source: Internet
Author: User

Reading notes based on the "Android Heroes ' biography"

View Measurement-onmeasure ()

The modes of measurement can be as follows three kinds:
exactly
That is, the exact value pattern, when we specify the Layout_width property or the Layout_height property of the control as a specific parameter value, the system uses the exactly
At_most
That is, the maximum mode, when the control's Layout_width property or the Layout_hright property is Warp_content, the size of the control generally changes as the control's child controls or contents change, and the size of the control does not exceed the size allowed by the parent control.
UNSPECIFIED
The size of the measurement mode is not specified, and the view is as large as it would normally be when the view is drawn.

The default Onmeasure () method of the view class supports only exactly mode, so if you do not override the Onmeasure () method when you customize the control, you can only use exactly mode

View Drawing--ondraw ()

To draw the corresponding image on the Android interface, you must draw it on the canvas. Canvas is like an artboard, and you can paint on it with paint.
In general, you can use the OnDraw () method in the Override view class to draw, and there is a parameter in OnDraw () that is the canvas canvas object that you can use to draw the object.

Measurement of ViewGroup

ViewGroup back to manage its child view, one of the management items is responsible for the display size of the child view. When the size of the ViewGroup is warp_content, ViewGroup needs to traverse the child view to get the size of all the child view, determine its size, and in other modes, set its size by specific designations.
ViewGroup the measurement of each sub-view by traversing all the sub-view in the measurement, thus invoking the measure method of the sub-view to obtain the measured result of each one, the previous measurement of the view is executed here.

Drawing of ViewGroup

ViewGroup usually does not need to draw, because it does not need to draw something, if not the background color of the specified ViewGroup, then the ViewGroup OnDraw () method will not be called. However, ViewGroup uses the Dispatchdraw () method to draw its child view, and the process is done by iterating through all the sub-view and invoking the drawing method of the child view to complete the drawing work.

Custom View

When customizing view, we usually go back and rewrite the OnDraw () method to draw the view's display. If the view also needs to use the Wrap_content property, you must also override the Onmeasure () method. In addition, you can set a new property value by customizing the Attrs property.

Extensions to existing controls
  1.对TextView进行扩展  2.闪动的文字效果
To create a composite control
  TopBar  1.定义属性    系统提供了TypedArray这样的数据结构来获取自定义的属性集--通     过一个方法,将你在atts.xml中定义的declare-styleable的所有    属性的值存储到TypedArray中。需要注意的是,当获取完所有属性  2.组合控件  3.引用UI模板    在引用前必须指定引用第三方控件的名字空间。
View to implement a new control
  创建一个自定义view的难点在于绘制控件和实现交互。  通常需要继承View类,并重写它的onDraw(),onMeasure()等方法来  实现绘制逻辑,同时需要重写onTouchEvent()等触控事件来实现交互    逻辑
Custom ViewGroup
  侧滑效果

Android control architecture and custom controls

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.