About custom components-use labelview in apidemo

Source: Internet
Author: User

You can use either of the following methods to inherit the View, override the onDraw () function, or inherit the ViewGroup or subclass to customize the components of the combination type.

The following is an example of apidemo.

The following is the code, and I have re-commented it. It should be easy to understand.

1. for constructor, the constructor of a single parameter is used when the code is new, and the second constructor is used for system reflection as an object, to use the constructor that must have two parameters in xml.

2. requestLayout (); this method applies the new layout, but the interface has not been refreshed yet.

3. invalidate (); this is used to notify the interface to refresh. If this method is called, it may not be refreshed immediately. It will be refreshed at a future moment, and the onDraw () method will be called during refresh.

 

Some details have not been written yet. I will write them here first, and I will try again later.

 

Understanding these Apis helps you understand the process of custom components

View

Http://note.youdao.com/share? Id = 1fcf0de6c7bbfec507fdb2b0962708ab & type = note

MessureSpec

Http://note.youdao.com/share? Id = b992b2b83cd8ff3245ad5609a9499966 & type = note

Paint

Http://note.youdao.com/share? Id = 735734a14ff01d96690f5c845f18ec7a & type = note

 

/** Copyright (c) 2007 the android open source project ** licensed under the Apache license, version 2.0 (the "License "); * You may not use this file before t in compliance with the license. * You may obtain a copy of the license at ** http://www.apache.org/licenses/LICENSE-2.0 ** unless required by applicable law or agreed to in writing, software * distributed under the license is distributed on An "as is" basis, * Without warranties or conditions of any kind, either express or implied. * See the license for the specific language governing permissions and * limitations under the license. */package COM. example. android. APIS. view; // need the following import to get access to the app resources, since this // class is in a sub-package.import android. content. context; import android. content. res. t Ypedarray; import android. graphics. canvas; import android. graphics. paint; import android. util. attributeset; import android. view. view; import COM. example. android. APIS. r;/*** example of how to write a custom subclass of view. labelview * is used to draw simple text views. note that it does not handle * styled text or right-to-left writing systems. **/public class labelview extends view {private paint Mtextpaint; private int mascent; private string mtext;/*** constructor. this version is only needed if you will be instantiating * The object manually (not from a layout XML file ). * @ Param context */Public labelview (context) {super (context); initlabelview ();}/*** construct object, initializing with any attributes we understand from a * layout file. these attributes are defined in * SDK/assets/RES/Any/classes. XML. ** @ see android. view. view # view (Android. content. context, android. util. attributeset) */Public labelview (context, attributeset attrs) {super (context, attrs); initlabelview (); typedarray A = context. obtainstyledattributes (attrs, R. styleable. labelview); charsequence S =. getstring (R. styleable. labelview_text); If (s! = NULL) {settext (S. tostring ();} // retrieve the color (s) to be used for this view and apply them. // note, if you only care about supporting a single color, that you // can instead call. getcolor () and pass that to settextcolor (). settextcolor (. getcolor (R. styleable. labelview_textcolor, 0xff000000); int textsize =. getdimensionpixeloffset (R. styleable. labelview_textsize, 0); If (textsize> 0) {Settextsize (textsize);}. recycle ();} private final void initlabelview () {// create text paint to setting text mtextpaint = new paint (); mtextpaint. setantialias (true); // you can specify whether the image is anti-aliasing. // must manually scale the desired text size to match screen density mtextpaint. settextsize (16 * getresources (). getdisplaymetrics (). density); mtextpaint. setcolor (0xff000000); setpadding (3, 3, 3, 3);}/*** sets the Tex T to display in this label * @ Param text the text to display. this will be drawn as one line. */Public void settext (string text) {mtext = text; // call this when something has changed which has invalidated the layout of this view. this will schedule a layout pass of the view tree. // when something has changed and the layout of the view is invalid, this method will arrange a new layout in the view tree. Requestlayout (); // invalidate the whole view. if the view is visible, ondraw (Android. graphics. canvas) will be called at some point in the future. this must be called from a UI thread. to call from a non-UI thread, call postinvalidate (). // call this method to make the current view display invalid. When this view is visible, ondraw (Android. graphics. canvas). This method must be called in the UI thread. If the postinvalidate () method is called from a non-UI thread, it is changed to a new display. Invalidate ();}/*** sets the text size for this label * @ Param size font size */Public void settextsize (INT size) {// This text size has been pre-scaled by the getdimensionpixeloffset method mtextpaint. settextsize (size); requestlayout (); invalidate ();}/*** sets the text color for this label. * @ Param color argb value for the text */Public void settextcolor (INT color) {mtextpaint. setcolor (Color); invalidate ();}/*** @ see android. view. view # Measure (INT, INT) */@ override protected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {// This Mehod must be called by onmeasure (INT, INT) to store the measured width and measured height. failing to do so will trigger an exception at measurement time. // This method must be called in onmeasure (INT, INT) to store the width and height of the measurement. If this parameter is not called, an exception is thrown during the measurement. Setmeasureddimension (measurewidth (widthmeasurespec), measureheight (heightmeasurespec ));} /*** determines the width of this view * @ Param measurespec A measurespec packed into an int * @ return the width of the view, honoring constraints from measurespec */private int measurewidth (INT measurespec) {int result = 0; // return Measurement Mode int specmode = measurespec. getmode (measurespec); // returns the measured size int specsize = Measurespec. getsize (measurespec); // If the mode is accurate, the size can only be the specified size, that is, the size obtained through the passed mode. If (specmode = measurespec. exactly) {// we were told how big to be result = specsize;} else {// measure the text // calculate the size of this view: size = left and right space size + String size result = (INT) mtextpaint. measuretext (mtext) + getpaddingleft () + getpaddingright (); If (specmode = measurespec. at_most) {// respect at_most value if that was what is called for by measurespec // In the at_most mode, that is, in the maximum mode, the size can only be smaller than or equal to the specified size, take the specified size and calculated size. The smallest of them. Result = math. min (result, specsize) ;}} return result ;} /*** determines the height of this view * @ Param measurespec A measurespec packed into an int * @ return the height of the view, honoring constraints from measurespec */private int measureheight (INT measurespec) {int result = 0; int specmode = measurespec. getmode (measurespec); int specsize = measurespec. getsize (measurespec); mascent = (in T) mtextpaint. ascent (); // returns the size of the upper part of the font baseline, returns a negative number // about the font baseline, can you see my http://note.youdao.com/share/ for this translation? Id = 735734a14ff01d96690f5c845f18ec7a & type = Note if (specmode = measurespec. exactly) {// we were told how big to be result = specsize;} else {// measure the text (beware: ascent is a negative number) // Height = size of the upper part of the baseline + size of the lower part of the baseline + Result = (INT) (-mascent + mtextpaint. descent () + getpaddingtop () + getpaddingbottom (); If (specmode = measurespec. at_most) {// respect at_most value if that was what is called for by measurespec result = math. min (result, specsize) ;}} return result;}/*** render the text *** @ see android. view. view # ondraw (Android. graphics. canvas) */@ override protected void ondraw (canvas) {super. ondraw (canvas); canvas. drawtext (mtext, getpaddingleft (), getpaddingtop ()-mascent, mtextpaint );}}

 

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.