Creating a View Class
A well-designed user interface should encapsulate a series of functions like other well-designed classes, and expose a simple interface to the user, and be able to use CPU and memory efficiently.
To design a custom View, we should:
Follow android development principles
You can find the setting attribute in the XML file.
Send events that can be captured
Compatible with multiple android platforms
The Android Framework provides a series of basic classes and XML nodes to create a View that meets the requirements. This section describes how to use the Android Framework to implement core view functions.
Subclass a View
All classes displayed in the Android Framework inherit from the View class. Custom classes can inherit the View directly or subclass of the View, such as the Button class, which saves time.
To make Android Developer Tools interact with custom views, a constructor containing Context and AttributeSet as parameters should be provided to the minimum. This constructor allows layout editor to create an instance of this custom View.
ClassPieChartextendsView {
PublicPieChart (Context context, AttributeSet attrs ){
Super (context, attrs );
}
}
Define Custom Attributes
You can use the node attributes of the Xml file to control the display and behavior of custom views. A good custom View can be used to add and manage attributes through an XML file.
To implement this custom View, you must:
Define custom attributes under the <declare-styleable> node of the resource file
Specify the attribute value in the XML layout file.
The property value can be obtained during running.
Re-define the attribute value of the custom view distributor
This section discusses how to define custom attributes and specify values for them. The next section mainly deals with how to get attribute values at runtime and assign values to attributes again.
To define custom attributes, add the <declare-styleable> node under the resource Node of the res/values/attrs. xml file.
The following is an example of the attrs. xml file:
<Resources>
<Declare-styleable name = "PieChart">
<Attr name = "showText" format = "boolean"/>
<Attr name = "labelPosition" format = "enum">
<Enum name = "left" value = "0"/>
<Enum name = "right" value = "1"/>
</Attr>
</Declare-styleable>
</Resources> the preceding Code declares two custom attributes: showText and labelPosition. Both attributes belong to the object view named PieChart. By convention, the object view name should be the same as the custom view name in the layout file. Although there are no clear requirements that must be the same, many codes are named like this.
Once these custom attributes are defined, you can use these attributes in the XML layout file. The usage of these attributes is the same as that of android embedded attributes. The only difference is that the namespace is different. The namespace method is http://schemas.android.com/apk/res/#your package name] replacing the previous http://schemas.android.com/apk/res/android
For example, the Code shows how to use the Custom Attributes in PieChart.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: custom = "http://schemas.android.com/apk/res/com.example.customviews">
<Com. example. customviews. charting. PieChart
Custom: showText = "true"
Custom: labelPosition = "left"/>
</LinearLayout> to avoid repeated use of such a long URI, The xmlns syntax rule used in this example is extracted.
Note that if the view to be used belongs to the nested type, you must add the Class Name of the external view. For example, PieChart has an internal class named PieView. If you want to use the PieView attributes, use com. example. customviews. charting. PieChart $ PieView.
Apply Custom Attributes
When a view is created, all attributes are read to the View constructor AttributeSet in China and Russia. However, reading data directly from AttributeSet has the following Disadvantages:
· Resource references within attribute values are not resolved
· Styles are not applied
Therefore, the use of the array of TypedArray replaces the use of AttributeSet and obtainStyledAttributes ().
The Android resource compiler has done a lot of work, making it easier to call obtainStyledAttributes.
For each <declare-styleable>, the R. java file automatically generates an attributes array and the position of each attribute in the array. You can use a pre-defined position in attributes to get attributes from TypeArray.
The following code demonstrates how to read its attributes from PieChart:
Public PieChart (Context context, AttributeSet attrs ){
Super (context, attrs );
TypedArray a = context. getTheme (). obtainStyledAttributes (
Attrs,
R. styleable. PieChart,
0, 0 );
Try {
MShowText = a. getBoolean (R. styleable. PieChart_showText, false );
MTextPos = a. getInteger (R. styleable. PieChart_labelPosition, 0 );
} Finally {
A. recycle ();
}
} Note that TypedArray is a shared resource and must be recycled after use.
Add Properties and Events
Attributes is powerful in controlling view display and behavior. However, this method can only be read when the view is initialized. To provide dynamic behavior, add the getter and setter methods for custom attributes. The following section demonstrates how PieChart exposes the showText attribute:
Public boolean isShowText (){
Return mShowText;
}
Public void setShowText (boolean showText ){
MShowText = showText;
Invalidate ();
RequestLayout ();
} Note: The setShowText method calls the invalidate () and requestLayout () methods. This method ensures that the operation can be executed normally. When the attribute and appearance of a view change, it is necessary to call the invalidate method to notify the System of the re-painting operation. Similarly, when the attribute changes, you also need to create a new layout to adapt to the current size and shape. If you forget to call these methods, it is difficult to find bugs.
Custom view should also support the listener to transmit messages. For example, PeiChat exposes an OnCurrentItemChanged method to notify the listener that the user has rotated the image to generate a new image.
It is easy to forget to expose attributes and events, especially when we are users of custom views. Now it takes some time to define the view Interface to reduce the maintenance cost. A good design principle is to expose all attributes that can change the display and behavior of custom views.
Design For Accessibility
Your custom view shocould support the widest range of users. This includes des users with disabilities that prevent them from seeing or using a touchscreen. To support users with disabilities, you shoshould:
· Label your input fields using the android: contentDescription attribute
· Send accessibility events by calling sendAccessibilityEvent () when appropriate.
· Support alternate controllers, such as D-pad and trackball
For more information on creating accessible views, see Making Applications Accessible in the Android Developers Guide.