14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: Creating User Interfaces<br />

Creating Custom Widgets and Controls<br />

88<br />

Creating completely new Views gives you the power to fundamentally shape the way your applications<br />

look and feel. By creating your own controls, you can create User Interfaces that are uniquely<br />

suited to your users’ needs. To create new controls from a blank canvas, you extend either the View or<br />

SurfaceView classes.<br />

The View class provides a Canvas object and a series of draw methods and Paint classes, to create<br />

a visual interface using raster graphics. You can then override user events like screen touches or key<br />

presses to provide interactivity. In situations where extremely rapid repaints and 3D graphics aren’t<br />

required, the View base class offers a powerful lightweight solution.<br />

The SurfaceView provides a canvas that supports drawing from a background thread and using<br />

openGL for 3D graphics. This is an excellent option for graphics-heavy controls that are frequently<br />

updated or display complex graphical information, particularly games and 3D visualizations.<br />

This chapter introduces 2D controls based on the View class. To learn more about the SurfaceView<br />

class and some of the more advanced Canvas paint features available in Android, see Chapter 11.<br />

Creating a New Visual Interface<br />

The base View class presents a distinctly empty 100 × 100 pixel square. To change the size of the control<br />

and display a more compelling visual interface, you need to override the onMeasure and onDraw<br />

methods, respectively.<br />

Within onMeasure, the new View will calculate the height and width it will occupy given a set of boundary<br />

conditions. The onDraw method is where you draw on the Canvas to create the visual interface.<br />

The following code snippet shows the skeleton code for a new View class, which will be examined<br />

further in the following sections:<br />

public class MyView extends View {<br />

// Constructor required for in-code creation<br />

public MyView(Context context) {<br />

super(context);<br />

}<br />

// Constructor required for inflation from resource file<br />

public MyView (Context context, AttributeSet ats, int defaultStyle) {<br />

super(context, ats, defaultStyle );<br />

}<br />

//Constructor required for inflation from resource file<br />

public MyView (Context context, AttributeSet attrs) {<br />

super(context, attrs);<br />

}<br />

@Override<br />

protected void onMeasure(int wMeasureSpec, int hMeasureSpec) {<br />

int measuredHeight = measureHeight(hMeasureSpec);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!