Build Custom Components

Source: Internet
Author: User

Today, I think that there is a custom component in Main. XML in layout in fbreaderj. Due to the frequent errors in the usage of scroller, I found the build method of the custom component.

The article is as follows:

 

Build Custom Components

In Android, your applications have a fixed connection with the view components, such as buttons, textviews, and edittext ), list box (listview), check box (checkbox), single box (radiobutton), scroll bar (Gallery), spinner, and so on, there are some more advanced view components with special purposes, for example, autocompletetextview, imageswitcher, and textswitcher. In addition, a wide variety of layout components, such as linearlayout and framelayout, are also considered as view components, they are derived from the View class.

Your application displays these control components and layout components on the screen in a certain way. Generally, these components are enough for you, however, you should also know that you can use class inheritance to create your own components. Generally, You can inherit components such as view and layouts (layout components, it can even be a relatively advanced control component. Next we will explain why inheritance is required:

  • You can create a completely custom component to implement a certain function. For example, you can use a two-dimensional Graphic creation control component to implement sound control, just like electronic control.
  • You can combine several components to form a new component. Your component may contain both ComboBox (a list of input texts) and dual-pane selector control (left and right list windows, you can allocate the subordination of each item in the window.
  • You can create your own layout component (layout ). The layout component in the SDK provides a series of options for you to build your own applications, however, senior developers will find it necessary to develop new layout components based on existing layout components, or even completely develop new components from the underlying layer.
  • You can overwrite the display or function of an existing component. For example, you can change the display mode of the edittext (editable text) component on the screen (refer to the notepad example to learn how to create an underline display page ).
  • You can capture events like pressing keys and handle them in some common ways (a game example)

Basic method (the basic approach)
Full custom components (fully customized components)
Example of custom components (customized component example)
Component mixing (or control class mixing) (Compound components (or compound controls ))
Modify existing components (tweaking an existing component)

 

Basic method (the basic approach)
The following steps are summarized to teach you how to create your own components:

1. Let your class inherit an existing View class or view subclass.
2. Methods for reloading the parent class: the parent class methods to be overloaded generally start with 'on', such as ondraw (), onmeasure (), and onkeydown.
* This is also applicable to activity or listactivity derivation. You need to reload some lifecycle functions and some other functional hook functions.
3. use your inheritance class: Once your inheritance class is created, you can use your inheritance class where the base class can be used, but the completion function is compiled by yourself.

Full custom components (fully customized components)

A Fully customized component can be used to create graphical components for display. It may be a graphical meter like a voltmeter, or a ball that shows the lyrics in karaoke will scroll with music. In either way, you cannot simply use the combination of components, no matter how you combine these existing components.

Fortunately, you can easily create your own components as required, you will find that your imagination, screen size, and processor performance are insufficient (remember that your application will only run on platforms with lower performance than your desktop ).

The following describes how to create a fully custom component:

1. The parent class of the most common view class is undoubtedly the View class. Therefore, you must create a subclass based on this class at the beginning.
2. you can write a constructor to extract attributes and parameters from the XML file. Of course, you can also define these attributes and parameters by yourself (maybe the color and size of the graphic measuring tool, or the width and amplitude of the pointer)
3. You may need to write your own event listener, attribute access and modify functions, and some code on the functions of some components.
4. If you want the component to display something, you are likely to overload the onmeasure () function, so you have to overload the ondraw () function. When both functions use the default one, the ondraw () function will not do anything, and the default onmeasure () function automatically sets a size of 100x100, this size may not be what you want.
5. Other functions of the on... series that need to be reloaded must be rewritten once.

Ondraw () and onmeasure ()

The ondraw () function will pass you a canvas object through which you can do anything on a two-dimensional graph, including some other standard and common components and text formats, anything you can think of can be implemented through it.

Note: 3D images are not included here. If you want to use 3D images, you should change your parent class from view to surfaceview class and use a separate thread. See the glsurfaceviewactivity example.

Onmeasure () functions are a bit tricky, because this function is a key part of the interaction between components and containers. onmeasure () should be overloaded, so that it can effectively and accurately represent the measured values of the part it contains. This is a bit complicated, because we should not only consider the restrictions of the parent class (passed through onmeasure (), but also know that once the width and height of the measurement come out, call the setmeasureddimension () method immediately.

To sum up, the onmeasure () function is executed in the following stages:

1. the overloaded onmeasure () method will be called, and the parameters of height and width will also involve (widthmeasurespec and heighmeasurespec are both integer types). You should also consider the size restrictions of your product. For more information, see view. onmeasure (INT, INT) (this connection details the entire measurement operation ).
2. your component needs to calculate the necessary measurement length and width through onmeasure () to display your component. It should be consistent with the specification, although it can implement functions other than the regular (in this example, the parent class can choose what to do, including cutting, sliding, submitting exceptions, or calling onmeasure () again with different parameters () function ).
3. Once the height and width are calculated, you must call setmeasureddimension (INT width, int height). Otherwise, an exception occurs.

Example of a custom component (a customized component example)

In API demos, customview provides an example of a custom component, which is defined in the labelview class.

The labelview example involves all aspects of custom components:

* First, let the custom component be derived from the View class.
* Compile a constructor with parameters (the parameters can come from an XML file ). Some of the above operations have been completed in the view parent class, but some custom components used by labelview have new parameters that need to be processed.
* Some standard public functions, such as settext (), settextsize (), and settextcolor ()
* The onmeasure () method is used to determine the component size (Note: In labelview, a private function measurewidth () is used)
* The ondraw () function is used to display the lable on the provided canvas.

In this example, you can use custom_view_1.xml to view the usage of the custom component labelview. In the XML file, pay special attention to the hybrid use of Android: And app: parameters. app: parameters indicate that the application is considered an individual of the labelview component, these are also defined as resources in the r class.

Code Analysis:

 

 

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.