Android Custom Control Series Tutorial----View

Source: Internet
Author: User

Understanding Android ViewThe area we see for Android is actually very much related to how it is drawn at the bottom, and most of the time we are only concerned with what we see, so what is it like at the bottom? Let's take a look at this diagram first.
For the entire device's visible area is actually the middle of our screen, from the above to take a picture can be clearly seen, in addition to our visible area in its upper and lower left and right should have content, then in the Android system is how to control the location of the display it? Let's answer the question below. How Android controls where views are displayed we can open the source of the view class to find these two functions
  /**     * Set The scrolled position of your view. This would cause a call to     * {@link #onScrollChanged (int, int, int, int)} and the view would be     * invalidated.     * @param x position     to scroll to * @param y the y position     to scroll to */public    void ScrollTo (int x, I NT y) {        if (mscrollx! = x | | mscrolly! = y) {            int oldx = MSCROLLX;            int oldY = mscrolly;            MSCROLLX = x;            mscrolly = y;            Invalidateparentcaches ();            Onscrollchanged (MSCROLLX, mscrolly, OLDX, OldY);            if (!awakenscrollbars ()) {                postinvalidateonanimation ();}}    }
There's another one.
   /**     * Move The scrolled position of your view. This would cause a call to     * {@link #onScrollChanged (int, int, int, int)} and the view would be     * invalidated.     * @param x The amount of pixels to scroll by horizontally     * @param y the amount of pixels to scroll by vertically     */Public    void Scrollby (int x, int y) {        scrollTo (mscrollx + x, mscrolly + y);    }
Let's take a closer look at the functions above. This source code I was excerpted from 5.0 source. We see Scrollby This function is also called Scrollto we're going to analyze what the Scrollto function does? A very simple code, the most important sentence is this sentence postinvalidateonanimation (); This code will go back to our OnDraw function, draw our visible area in the OnDraw function, and then we'll look at the method of draw of the view
  * Manually render this view (and all of it children) to the given Canvas.  * The view must has already do a full layout before this function is * called.     When implementing a view, implement * {@link #onDraw (Android.graphics.Canvas)} instead of overriding this method.     * If You don't need to override this method, call the superclass version.     * * @param canvas the canvas to which the View is rendered.        */public void draw (canvas canvas) {if (mclipbounds! = null) {canvas.cliprect (mclipbounds);        } final int privateflags = mprivateflags; Final Boolean dirtyopaque = (Privateflags & pflag_dirty_mask) = = Pflag_dirty_opaque && (mattach Info = = NULL | |        !mattachinfo.mignoredirtystate); Mprivateflags = (privateflags & ~pflag_dirty_mask) |        Pflag_drawn;         /* * Draw Traversal performs several drawing steps which must be executed * in the appropriate order:       *  * 1. Draw the background * 2. If necessary, save the canvas ' layers to prepare for fading * 3. Draw View ' s content * 4. Draw Children * 5. If necessary, draw the fading edges and restore layers * 6. Draw decorations (scrollbars for instance) *//Step 1, draw the background, if needed int Savecoun        T            if (!dirtyopaque) {final drawable background = Mbackground;                if (background! = NULL) {final int scrollx = MSCROLLX;                Final int scrolly = mscrolly;                    if (mbackgroundsizechanged) {background.setbounds (0, 0, Mright-mleft, mbottom-mtop);                Mbackgroundsizechanged = false;                } if ((Scrollx | scrolly) = = 0) {background.draw (canvas);                    } else {canvas.translate (scrollx, scrolly); Background.draw (Canvas);                Canvas.translate (-SCROLLX,-scrolly);        }}}//Skip step 2 & 5 if possible (common case) Final int viewflags = mviewflags;        Boolean horizontaledges = (ViewFlags & fading_edge_horizontal)! = 0;        Boolean verticaledges = (ViewFlags & fading_edge_vertical)! = 0; if (!verticaledges &&!horizontaledges) {//Step 3, draw the content if (!dirtyopaque) Ondr            AW (canvas);            Step 4, Draw the children Dispatchdraw (canvas);            Step 6, Draw decorations (scrollbars) ondrawscrollbars (canvas);            if (moverlay! = null &&!moverlay.isempty ()) {Moverlay.getoverlayview (). Dispatchdraw (canvas);        }//we ' re done ... return;          }/* * Here we do the full fledged routine ... * (this was an uncommon case where speed matters less, * This is WHY we repeat some of the tests that has been * done above) */Boolean drawtop = false;        Boolean drawbottom = false;        Boolean drawleft = false;        Boolean drawright = false;        float topfadestrength = 0.0f;        float bottomfadestrength = 0.0f;        float leftfadestrength = 0.0f;        float rightfadestrength = 0.0f;        Step 2, save the canvas ' layers int paddingleft = Mpaddingleft;        Final Boolean offsetrequired = Ispaddingoffsetrequired ();        if (offsetrequired) {paddingleft + = Getleftpaddingoffset ();        } int left = Mscrollx + paddingleft;        int right = left + Mright-mleft-mpaddingright-paddingleft;        int top = mscrolly + getfadetop (offsetrequired);        int bottom = top + getfadeheight (offsetrequired);            if (offsetrequired) {right + = Getrightpaddingoffset ();        Bottom + = Getbottompaddingoffset (); } Final Scrollabilitycache ScrollabiliTycache = Mscrollcache;        Final float fadeheight = scrollabilitycache.fadingedgelength;        int length = (int) fadeheight; Clip the fade length if top and bottom fades overlap//overlapping fades produce odd-looking artifacts I        F (verticaledges && (top + length > Bottom-length)) {length = (bottom-top)/2; }//also clip horizontal fades if necessary if (Horizontaledges && (left + length > Right-leng        th)) {length = (right-left)/2;            } if (verticaledges) {topfadestrength = Math.max (0.0f, Math.min (1.0f, Gettopfadingedgestrength ()));            Drawtop = topfadestrength * fadeheight > 1.0f;            Bottomfadestrength = Math.max (0.0f, Math.min (1.0f, Getbottomfadingedgestrength ()));        Drawbottom = bottomfadestrength * fadeheight > 1.0f; } if (horizontaledges) {leftfadestrength = Math.max (0.0f, Math.min (1.0f, GETLEFTFADingedgestrength ()));            Drawleft = leftfadestrength * fadeheight > 1.0f;            Rightfadestrength = Math.max (0.0f, Math.min (1.0f, Getrightfadingedgestrength ()));        Drawright = rightfadestrength * fadeheight > 1.0f;        } Savecount = Canvas.getsavecount ();        int solidcolor = Getsolidcolor ();            if (Solidcolor = = 0) {Final int flags = Canvas.has_alpha_layer_save_flag;            if (drawtop) {Canvas.savelayer (left, top, right, top + length, NULL, flags);            } if (Drawbottom) {Canvas.savelayer (left, bottom-length, right, bottom, NULL, flags);            } if (Drawleft) {Canvas.savelayer (left, top, left + length, bottom, NULL, flags);            } if (drawright) {Canvas.savelayer (right-length, top, right, bottom, NULL, flags);        }} else {Scrollabilitycache.setfadecolor (solidcolor);      }  Step 3, Draw the content if (!dirtyopaque) OnDraw (canvas);        Step 4, Draw the children Dispatchdraw (canvas);        Step 5, draw the fade effect and restore layers final Paint p = scrollabilitycache.paint;        Final Matrix matrix = Scrollabilitycache.matrix;        Final Shader fade = Scrollabilitycache.shader;            if (drawtop) {Matrix.setscale (1, fadeheight * topfadestrength);            Matrix.posttranslate (left, top);            Fade.setlocalmatrix (matrix);        Canvas.drawrect (left, top, right, top + length, p);            } if (Drawbottom) {Matrix.setscale (1, fadeheight * bottomfadestrength);            Matrix.postrotate (180);            Matrix.posttranslate (left, bottom);            Fade.setlocalmatrix (matrix);        Canvas.drawrect (left, bottom-length, right, bottom, p);            } if (Drawleft) {Matrix.setscale (1, fadeheight * leftfadestrength); Matrix.postrotate (-90);            Matrix.posttranslate (left, top);            Fade.setlocalmatrix (matrix);        Canvas.drawrect (left, top, left + length, bottom, p);            } if (drawright) {Matrix.setscale (1, fadeheight * rightfadestrength);            Matrix.postrotate (90);            Matrix.posttranslate (right, top);            Fade.setlocalmatrix (matrix);        Canvas.drawrect (Right-length, top, right, bottom, p);        } canvas.restoretocount (Savecount);        Step 6, Draw decorations (scrollbars) ondrawscrollbars (canvas);        if (moverlay! = null &&!moverlay.isempty ()) {Moverlay.getoverlayview (). Dispatchdraw (canvas); }    }


You can see out our mscrollx and mscrolly and then the various translate drawings in the drawing so that we see the contents of the view, which is the general idea. Display area instances now take an example to understand the problem. Let's go straight to the very simple code.
LinearLayout linearlayout = new LinearLayout (this); Linearlayout.setlayoutparams (New Layoutparams (2000, 2000));// Here I set the width of the high point to do a test because my phone is 1920*1080 so set a little linearlayout.setorientation (linearlayout.horizontal); TextView textView1 = new TextView (this); Textview1.settext ("Hello I am text 1"); Textview1.setlayoutparams (new Layoutparams (+)); Linearlayout.addview (TextView1); TextView textView2 = new TextView (this); Textview2.settext ("Hello I am text 2"); Textview2.setlayoutparams (new Layoutparams (+), Linearlayout.addview (TEXTVIEW2); Setcontentview (LinearLayout);

is to initialize our interface in the OnCreate function of the activity. We gave it a big view, bigger than the phone screen, and then we ran to see what the effect would be. We can see clearly that our TextView2 is not showing up, and it is very clear here, because beyond the screen, and then we re-add a line of code.
LinearLayout linearlayout = new LinearLayout (this); Linearlayout.setlayoutparams (New Layoutparams (2000, 2000));// Here I set the width of the high point to do a test because my phone is 1920*1080 so set a little linearlayout.setorientation (linearlayout.horizontal); TextView textView1 = new TextView (this); Textview1.settext ("Hello I am text 1"); Textview1.setlayoutparams (new Layoutparams (+)); Linearlayout.addview (TextView1); TextView textView2 = new TextView (this); Textview2.settext ("Hello I am text 2"); Textview2.setlayoutparams (new Layoutparams (+)); Linearlayout.addview (TEXTVIEW2); Linearlayout.scrollto (1000, 0);// We added this code setcontentview (linearlayout);

You can see that the Text2 display is complete, which is also obvious because we have scrolled the view to the left. A series of tutorials on the UI controls in the back, these two scrollto and Scrollby functions are used very frequently, so here we analyze it from the source once and help us understand it later.

Android Custom Control Series Tutorial----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.