About Android Developing custom view

Source: Internet
Author: User

Android app development process, often encountered in the system framework provided by the control can not meet the design needs of our products, then we can choose to first Google under the more mature open source projects can let us use, or GitHub above the project is also very rich, To meet our not-so-many development needs, but when using these cool third-party controls, we need to be in control of the details of the implementation, so it breeds today's "explore the custom view in Android development"

Bold is my personal question, the following small print is a combination of access to information and the actual operation of the inductive understanding.

What is view?

All Android controls are either view or subclass of view, which actually represents a rectangular area on the screen, represented by a rect, and Left,top represents the starting point of the view relative to its parent view, width, The height represents the view's own width, and with these 4 fields you can determine the position of the view on the screen, and then you can start to draw the view after you have determined the position.

What is the view drawing process?

The drawing of a view can be divided into the following three processes:

  1. Measure
    The view will do a measurement first to figure out how much area you need to occupy. The measure process of view exposes us to an interface onmeasure, which is defined by the method,

    protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {}

    The view class already provides a basic onmeasure implementation,

    protected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {setmeasureddimension (Getdefaultsize (Getsuggestedminimumwidth (), Widthmeasurespec), Getdefaultsize (getSuggested Minimumheight (), Heightmeasurespec));} Public Static intGetdefaultsize (intSizeintMeasurespec) {  intresult =size; intSpecmode =Measurespec.getmode (MEASURESPEC); intSpecsize =measurespec.getsize (MEASURESPEC); Switch(specmode) { CaseMeasureSpec.UNSPECIFIED:result=size;  Break;  CaseMeasurespec.at_most: CaseMeasureSpec.EXACTLY:result=specsize;  Break; }  returnresult;}

    Which invoke the Setmeasureddimension () method, set the measure process the width of the view, Getsuggestedminimumwidth () return to the view of the minimum width,height also has a corresponding method. We can rewrite the onmeasure to redefine the view's wide height.

  2. Layout

The layout process is very simple for the view class, and the same view exposes us to the OnLayout method

protected void OnLayout (boolean changed, int left, int top, int. right, int c7> bottom) {}

Because we're talking about view now, no sub-view needs to be arranged, so this step does not need to do extra work.

3. Draw

The draw process is to draw the view style that we need on the canvas. The same view exposes us to the OnDraw method.

protected void OnDraw (canvas canvas) {}

The default view class's OnDraw does not have a single line of code, but provides us with a blank canvas, like a picture scroll, we are painters, can draw what kind of effect, completely depends on us.

Methods that you see in the instance
    • Requestlayout
      The view re-invokes the layout process.

    • Invalidate
      View calls the draw process again

    • Forcelayout
      Identifies the next redraw of the view, which requires a recall of the layout procedure.

How is the layout file configured?

Custom control Properties, we all know that view has some basic properties, such as layout_width,layout_height,background, we often need to define their own properties, then we can do so.

    • 1. In the Values folder, open Attrs.xml, in fact, this file name can be arbitrary, write here more standard point, that is all the properties of the view is placed inside.
    • 2. Three properties are created below

      <declare-styleable name= "Rainbowbar" >  <attr name= "Rainbowbar_hspace" format= "Dimension" ></attr >  <attr name= "Rainbowbar_vspace" format= "Dimension" ></attr>  <attr name= "Rainbowbar_ Color "format=" color ></attr></declare-styleable>

About Android Developing custom view

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.