Fully
customized components can be used to create graphical components that
appear however you wish. Perhaps a graphical VU meter that looks like an
old analog gauge, or a sing-a-long text view where a bouncing ball
moves along the words so you can sing along with a karaoke machine.
Either way, you want something that the built-in components just won't
do, no matter how you combine them.
Fortunately,
you can easily create components that look and behave in any way you
like, limited perhaps only by your imagination, the size of the screen,
and the available processing power (remember that ultimately your
application might have to run on something with significantly less power
than your desktop workstation).
To create a fully customized component:
- The most generic view you can extend is, unsurprisingly,
View, so you will usually start by extending this to create your new super component. - You can supply a constructor which can take attributes and parameters from the XML, and you can also consume your own such attributes and parameters (perhaps the color and range of the VU meter, or the width and damping of the needle, etc.)
- You will probably want to create your own event listeners, property accessors and modifiers, and possibly more sophisticated behavior in your component class as well.
- You will almost certainly want to override
onMeasure()and are also likely to need to overrideonDraw()if you want the component to show something. While both have default behavior, the defaultonDraw()will do nothing, and the defaultonMeasure()will always set a size of 100x100 — which is probably not what you want. - Other
on...methods may also be overridden as required.
Extend onDraw() and onMeasure()
The
onDraw() method delivers you a Canvas
upon which you can implement anything you want: 2D graphics, other
standard or custom components, styled text, or anything else you can
think of.
No comments:
Post a Comment