Android Custom View Controls

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/lvwenbo0107/article/details/50542597

I've written enough detail.

Why do I want to customize a control

1. Specific display style.

2. Handle unique user interactions. (TextView supports some sliding functions)

3. Optimize our layout. (Nested layout drawing is slower)

4. Encapsulation. (tab button is not good to see)

How to customize a control

1. Custom attribute declarations and fetches.

2. Measure Onmeasure.

3. Layout OnLayout (ViewGroup only needed)

4. Draw OnDraw

5.onTouchEvent

6.onInterceptTouchEvent (ViewGroup)

I. Custom attribute declaration and acquisition


Two. Measuring onmeasure

The measured size is determined by the measurement mode and the measured value .

1. Mode:

exatly (Explicit value)

At_most (cannot exceed a value at most, typically in set wrap_content, the size of the control has its own decision but cannot exceed the size of the parent control)

UNSPECIFIED (unrestricted, such as the height of Scrollview,listview's child view)

2. Measured values

The Measurespec Helper class encapsulates the measurement mode with the measured value, which is passed down by the parent control, gets the value through the GetMode, and obtains the values through the GetSize

The obtained result requires a call to the setmeasureddimension

If you want to re-measure, the call provides a () {requestlayout () re-measurement of the measure and layout onlayout}

Invalidate Trigger Draw


Three. Layout OnLayout (viewgroup) parent control determines the display use of child controls (only once)

1. Determine the location of the sub-view.

2. Whenever possible, move some (time-consuming, initialization) operations in the Onmeasure to this method (only once).

3.requestLayout () to trigger onlayout


Four. Draw OnDraw

1. Draw the content area

2. Redraw: Invalite () UI thread Call Postinvalidate () child thread call;

3.canvas.drawxxx

4.translate Rotate scale skew (canvas transform)

5.save () Restore (save restore after transform)

Custom control If you do not interact with the user only need to consider onmeasure,ondraw, custom containers need to consider onmeasure,onlayout, generally do not consider OnDraw


Five. Ontouchevent

Velocitytracker used to track the rate of touch events (flinging events and other gesture events)

Pareent.requestdisallowintercepttouchevent (true) child control notifies the parent control not to intercept the entire gesture (Down->up) handles gestures by a sub-control

Action_point_down/up Multi-Touch

Six. Onintercepttouchevent (ViewGroup)

The event delivery mechanism under which the parent control is handed over to the child control to handle the event of the parent control being intercepted by the rights of the child control during the forwarding process.

Returning true represents the interception of child control events, which is handed to ViewGroup to handle itself.

Decide whether to intercept the gesture (down->up)

Seven. What else is there?

1.onSaveInstanceState: This is the specific reference for the control layer. Imitation 6.0 case Onchangeiconcolor.onrestoreinstancestate

2.ViewConfiguration: Gets the control constants. Eg:mtouchslop

3.ScaleGestureDetector Zoom gesture

....

Like what:

view-I'm going to analyze this view, he needs those custom attributes I'm going to write, and then I'm going to get the custom view constructor.

I'll rewrite the way he measured, determine the space our view occupies, and after the measurement is done, I'll make a copy of the OnDraw method to determine our view

Show in what form.

viewgroup-first measures the size of itself, as well as the size of the child view, and then the onlayout determines the rules of sub-view placement, if there is

User interaction needs to rewrite Ontouchevent, decide how to interact, onintercepttouchevent decide whether to intercept sub view.

Android Custom View 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.