Android custom view steps

Examples are as follows: Android custom view password box example

1 good custom view

Easy to use, standard, open.

A well-designed custom view is very similar to other well-designed classes. It encapsulates a function combination with easy-to-use interface, which can effectively use CPU and memory, and is very open. However, in addition to starting a well-designed class, a custom view should:

L Android compliant

L provide custom style attributes that can work in Android XML layout

L send accessible events

L compatible with multiple Android platforms.

The Android framework provides a set of basic classes and XML tags to help you create a new view that meets these requirements. It's easy to forget to provide properties and events, especially if you are the only user of this custom view. Please take some time to carefully define your view interface to reduce the time spent in future maintenance. A guideline to follow is to expose all attributes or behaviors in your view that affect the visual appearance.

2. Create a custom view (step)

2.1 fully customize or inherit the derived subclasses of view

A constructor that can obtain context and attributeset objects as attributes must be provided to obtain attributes. After the view is created from the XML layout, all attributes in the XML tag are read from the resource package and passed to the view as an attributeset.

Direct or indirect subclasses derived from view: ImageView, button, Check@R_ 748_ 2419@,SurfaceView,TextView,ViewGroup,AbsListView

Direct or indirect subclasses derived from viewgourp: absolutelayout, FrameLayout, relativelayout, LinearLayout

All base classes and derived classes are standard system classes integrated by the Android framework layer. You can directly reference these system classes and their APIs in the SDK

2.2 defining custom attributes

L define custom attributes for your view in the resource element < declare styleable >.

Add < declare styleable > resources to the project group. These resources are usually placed in the RES / values / attrs.xm file. The following is an example of the attrs.xml file:

L use the value of the specified attribute in your XML layout.

They can be used in layout XML files like built-in attributes. The only difference is that custom attributes belong to different namespaces.

http://schemas.android.com/apk/res/ [package path of your custom view]

L get the attribute value at run time.

L apply the retrieved attribute value to your view.

2.3 get custom attributes

After the view is created from the XML layout, all attributes in the XML tag are read from the resource package and passed to the view constructor as an attributeset. Although it is possible to read values directly from attributeset, this has some disadvantages:

L resource references with values are not processed

L style is not allowed

Instead, pass attributeset to the obtainstyledattributes () method. This method returns a typedarray array containing dereferenced and stylized values.

In order to call the obtainstyledattributes () method more easily, the Android resource compiler has done a lot of work. For each < declare styleable > resource in the res folder, the generated r.java defines an array of attribute IDs and a set of constants that point to each attribute in the array. You can use predefined constants to read properties from typedarry. The following example shows how the piechart class reads these attributes:

Note that the typedarry object is a shared resource and must be recycled after use.

2.4 adding attributes and events

Properties are a powerful way to control the behavior and appearance of a view, but they are readable only after the view is initialized. To provide dynamic behavior, you need to expose a pair of getters and setters for each custom property. The following code snippet shows how piechart provides the showtext property.

Notice that setshowtext calls invalidate () and requestlayout (). The key of these calls is to ensure that the view behavior is reliable. You must abolish the view after changing the attribute that may change the appearance, so that the system knows that it needs to redraw. Similarly, if changes in attributes may affect the size or shape of the view, you need to request a new layout. Forgetting to call these methods can lead to hard to find bugs.

Custom views also require event listeners that support communication with important events.

2.5 custom drawing (Implementation)

The most important step in drawing a custom view is to override the ondraw() method. The parameter of ondraw() is that the view can be used to draw its own canvas object. Canvas definition is used to draw text, lines, bitmaps and other image units. You can use these methods to create your custom user interface (UI) in ondraw()

The android.graphics framework divides the drawing into two parts:

L what to draw is handled by canvas

L how to draw, handled by paint

For example, canvas provides a method to draw lines, while paint provides a method to define the color of lines. Canvas provides a method to draw rectangles, and paint defines whether to fill the rectangle with color or make it empty. In short, canvas defines the shapes you can draw on the screen, and paint defines the color, style, font, etc. for each shape you draw

Ondraw() does not provide support for 3D graphics APIs. If you need 3D graphics support, you must inherit surfaceview instead of view and draw through a separate thread.

3 Optimization

3.1 reduce refresh rate

In order to improve the running speed of view and reduce unnecessary code from frequently called programs. Start calling from the OnDraw () method, which will bring you the best return. In particular, in the OnDraw () method, you should reduce redundant code, which will lead to garbage collection that makes your view incoherent. Redundant objects initialized, or between animations, will never contribute when the animation runs.

In addition, in order to make the OnDraw () method more dependent, you should call it as often as possible. Most of the time, calling OnDraw () method is the result of calling invalidate (), so unnecessary calls to invalidate () method are reduced. It is possible to call four different types of parameters, invalidate (), instead of calling the parameterless version. Parameterless variables need to refresh the entire view, while variables of the four parameter types only need to refresh the specified part of the view. This efficient call is closer to the demand and can also reduce unnecessary refreshed pages falling outside the rectangular screen.

3.2 using hardware acceleration

As Android 3.0, the Android 2D chart system can be added through the GPU (chart processing unit) of most new Android devices. For many applications, the GPU hardware acceleration can bring huge performance increase, but it is not the right choice for every application. The Android framework layer better provides you with the ability to control whether part of the hardware of the application is added.

How to use the acceleration class in your application, activity, or form level, please refer to the hardware acceleration class in the Android Developer's Guide. Note the additional instructions in the developer's Guide. You must set the application target API to 11 or higher in < uses SDK Android: targetsdkversion = "11" / > in your androidmanifest.xml file.

Once you use the hardware acceleration class, you may not see the growth of performance. The mobile GPUs is very good at some tasks, such as measuring, flipping, and shifting pictures. In particular, they are not good at other tasks, such as drawing lines and curves. In order to take advantage of GPU acceleration class, you should increase the number of operations that GPU is good at and reduce the number of operations that GPU is not good at.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>