Saturday, July 12, 2014

Android Custom Components


Creating own view subclasses gives an application precise control over the appearance and function of screen element. Below are the examples of usefulness of creating custom views

- Appplication create a completely custom-rendered view type, for e.g. a Volume control knob rendered using 2D graphics, and which resembles an analog electronic control. 
- Application could combine a group of view components into a new single components, perhaps to make something like a combo box (a combination of popup list and free entry text field), a dual-pane selector control (a left and right pane with a list in each where application can re-assign which item is in which list.) and so on
- Application could override the way that an EditText components is rendered on the screen 
- Application could capture other events like key presses and handle them in some custom way (such as for a game)

The basic approach to do this is like given below

1. Extend an existing View class or subclass with own class
2. Override some of the methods from the superclass. The superclass methods to override starte with on. for e.g. onDraw, onMeasure, onKeyDown, 
3. once above is completed, the new custom view class can be used in place of regular view

Fully customised components can be created like below 
A good example could be a sing-along-text view where a bouncing ball moves along the words so user can sign along with a karaoke machine. 

1. Extend from View. this is the most generic component from which a view can be derived. 
2. Supply a constructor which can take attributes and parameters from the XML
3. Probably create own event listeners, property accessors and modifiers, and possibly more sophisticated behavior
4. Most certainly override the onMeasure() and likely override the onDraw. The default onDraw will do nothing, and the default onMeasure() will always set the z size of 100x100 - Application may require more than this. 
5. Other on… methods are overridden as required. 

The onDraw method delivers application a Canvas upon which the application can draw anything wanted. 2D graphics or standard or custom components, styled text etc. But this can't render the 3D graphics, IF want the 3D graphics, needs to override the SurfaceView instead of View and draw from a separate thread. 

onMeasure is little more involved. This is a critical contract between the application component and its container. 

Below given is the logic that goes into the onMeasure method 

- The overridden onMeasure method is called with width and height measure specifications (widthMeasureSpec and heightMeasureSepc) parameters both representing the dimensions. These should be treated as requirements for the restrictions on the width and height measurements that component should produce. 
- The new Custom component's onMeasure method should calculate a measurement width and height which will be required to render the component. It should try to stay within the specifications passed in, although it can choose to exceed them (in this case, parent can choose what to do such as clipping, scrolling, throwing an exception or asking onMeasure to try again perhaps with a different measurement specifications) 
- Once the width and height are calculated, the setMeasuredDimension(int width, int height) method must be called with the calculated measurements. Failure to do this will result in exception being thrown. 

Rerefences: 

No comments:

Post a Comment