Android Elite Biography-Chapter III: Android Control architecture vs. custom controls

Source: Internet
Author: User
Tags extend

  • 3.1 Android Control architecture
  • Measurement of 3.2 view
  • Drawing of 3.3 view
  • Measurement of 3.4 ViewGroup
  • Drawing of 3.5 ViewGroup
  • 3.6 Customizing View
    • 3.6.1 to expand the existing space
    • 3.6.2 Creating a composite control
    • 3.6.3 rewrite view to achieve new space
  • 3.7 Custom ViewGroup
  • 3.8 event interception mechanism analysis
The

Control is roughly two types:

  • View Control: View Control
  • ViewGroup control: Contains multiple view controls and manages the closing between the view controls it contains
  • System: The upper control is responsible for measuring and drawing the underlying child controls and passing interactive events

UI interface Schema:

  • activity contains a Window object, usually implemented by Phonewindow
  • p Honewindow sets a decorview to the entire application window's root view
    • Decorview The topmost view of the entire window interface
    • decorview only one child element is Lin Earlayout, representing the entire window interface, including the notification bar, the title bar, the content display bar three block area
    • LinearLayout has two framelayout child elements:
      • title bar display interface. Only one TextView displays the name of the app
      • the content bar displays the interface. is the Setcontentview () method loading the layout interface

Measurespec class: 32-bit int value, high 2 bit for measurement mode, low 30 bit for measurement size

Measurespec mode:

  • Exactly: Precision mode, when the control's Layout_width property or Layout_height property is specified as a specific value, the control size is also the specific value
  • At_most: Maximum mode, when the control Layout_width property or Layout_height property is specified as Warp_content, the size of the control does not exceed the maximum size allowed by the parent control
  • UNSPECIFIED: No pattern is specified, how large the control is, and usually the custom view is not used

The default Onmeasure () method of the view class supports only the exactly mode, and the view needs to support the Warp_content property, then the Onmeasure () method must be overridden to make the warp_content size

Here's a simple example that shows how to measure a view, first of all, to rewrite the Onmeasure () method:

You can see that the Onmeasure method calls the Onmeasure method of the parent class, and the code tracks the parent class Onmeasure method

It can be found that the system will eventually call the setmeasureddimension (int measuredwidth,int measuredheight) method to set the measured width-height value in, we call the custom Measurewidth () Method and the Measureheight () method, respectively, to redefine the width and height

The following is an example of the Measurewidth () method:

Step One: Extract the specific measurement mode and size from the Measurespec object

Step two: Different measurements are given by different measurement modes:

  • Exactly: You can use the specified specsize
  • At_most: Take out the minimum value of the size and specsize we specify
  • unspecified:200px

The following code can basically be used as a template code:

You can see that when you specify the Warp_content property, the view gets a default value of 200px

After a view has been measured, we draw by rewriting the OnDraw () method in the view class to draw the corresponding image, which must be drawn on the canvas

The canvas is like an artboard, and we pass in a bitmap that is tightly tied to the canvas canvas created by this bitmap, which we call the loading canvas, the bitmap used to store all the pixel information drawn on the canvas, So when you call all the canvas.drawxxx methods in the back, it's going to happen on this bitmap.

ViewGroup The measure method of the child view to obtain the results of each sub-view by traversing all sub-view at the time of measurement

After the viewgroup has been measured, the logic that usually overrides the OnLayout () method to control its child view display position

ViewGroup usually does not need to be drawn, and if it is not the background color of the specified ViewGroup, then ViewGroup's OnDraw () method will not be called, but ViewGroup will use the Dispatchdraw () method to draw the child view

There are usually some important callback methods in view:

  • Onfinishinflate (): Callback after loading component from XML
  • Onsizechanged (): Callback when component size changes
  • Onmeasure (): Callback the method for measurement
  • OnLayout (): Callback the method to determine the location of the display
  • Ontouchevent (): callback when the monitor hears a touch event

Typically, there are three ways to implement a custom control:

  • Extend the existing controls
  • To implement a new control by combining
  • Override view to implement a new control

3.6.1 to extend existing controls

  • Custom Modify TextView ... See Classic code review, case one
  • Flashing text effect ... See Classic code review, case two

3.6.2 Creating a composite control

  • Customizing the implementation of the toolbar ... See Classic code review, case three

3.6.3 override view to implement a new control

  • ARC Display Diagram ... See Classic code review, case four
  • Audio Bar Chart ... See Classic code review, case five
  • Custom ViewGroup, Imitation scrollview ... See Classic code review, case six

Three important methods of event interception mechanism

    • Dispatchtouchevent (): Distributing events
    • Onintercepttouchevent (): Intercept event
    • Ontouchevent (): Handling Events

Give an example of the event distribution mechanism:

    • Viewgroupa: In the lower view
    • VIEWGROUPB: In the middle of the view
    • View: At the top

Normal event distribution mechanism flow:

    • Viewgroupa dispatchtouchevent
    • Viewgroupa onintercepttouchevent
    • VIEWGROUPB dispatchtouchevent
    • VIEWGROUPB onintercepttouchevent
    • View dispatchtouchevent
    • View ontouchevent
    • VIEWGROUPB ontouchevent
    • Viewgroupa ontouchevent

If the VIEWGROUPB onintercepttouchevent () method returns true for the distribution mechanism process:

    • Viewgroupa dispatchtouchevent
    • Viewgroupa onintercepttouchevent
    • VIEWGROUPB dispatchtouchevent
    • VIEWGROUPB onintercepttouchevent
    • VIEWGROUPB ontouchevent
    • Viewgroupa ontouchevent

If the view's Ontouchevent () method returns true for the distribution mechanism process:

    • Viewgroupa dispatchtouchevent
    • Viewgroupa onintercepttouchevent
    • VIEWGROUPB dispatchtouchevent
    • VIEWGROUPB onintercepttouchevent
    • View dispatchtouchevent
    • View ontouchevent

If the VIEWGROUPB ontouchevent () method returns true for the distribution mechanism process:

    • Viewgroupa dispatchtouchevent
    • Viewgroupa onintercepttouchevent
    • VIEWGROUPB dispatchtouchevent
    • VIEWGROUPB onintercepttouchevent
    • View dispatchtouchevent
    • View ontouchevent
    • VIEWGROUPB ontouchevent

Simply put, dispatchtouchevent () and Onintercepttouchevent () are distributed from bottom to top, while Ontouchevent () is distributed from the top down to the next level.

Create a Attrs.xml file in the Values folder to customize the properties

Start to create our toolbar

Using in the layout file

When the user does not specify a specific scale value, the following code can be called to set the corresponding scale value

Custom ScrollView No system comes with good performance, after all, many factors are not considered, this is only applicable to practiced hand use

Using in layouts

Classic Review Source download

Github:https://github.com/csdnhensen/qunyingzhuang

Android Elite Biography-Chapter III: Android Control architecture vs. custom controls

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.