blob: 8280237167bb0155fd0a4196522049e5f5b78f85 [file] [log] [blame]
page.title=Custom Drawing
parent.title=Creating Custom Views
previous.title=Creating a View Class
next.title=Making the View Interactive
<div id="tb-wrapper">
<div id="tb">
<h2>This lesson teaches you to</h2>
<li><a href="#ondraw">Override onDraw()</a></li>
<li><a href="#createobject">Create Drawing Objects</a></li>
<li><a href="#layoutevent">Handle Layout Events</a></li>
<li><a href="#draw">Draw!</a></li>
<h2>You should also read</h2>
<li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html">
Canvas and Drawables</a></li>
<h2>Try it out</h2>
<div class="download-box">
<a href="{@docRoot}shareables/training/"
class="button">Download the sample</a>
<p class="filename"></p>
<p>The most important part of a custom view is its appearance. Custom drawing can be easy or complex
according to your
application's needs. This lesson covers some of the most common operations.</p>
<h2 id="overrideondraw">Override onDraw()</h2>
<p>The most important step in drawing a custom view is to override the {@link
android.view.View#onDraw( onDraw()} method. The parameter to {@link
android.view.View#onDraw( onDraw()} is a {@link Canvas} object that the view can use to draw itself. The {@link Canvas}
class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can
use these methods in
android.view.View#onDraw( onDraw()} to create your custom user interface (UI).</p>
<p>Before you can call any drawing methods, though, it's necessary to create a {@link Paint}
object. The next section discusses {@link Paint} in more detail.</p>
<h2 id="createobject">Create Drawing Objects</h2>
<p>The {@link} framework divides drawing into two areas:</p>
<li><i>What</i> to draw, handled by {@link Canvas}</li>
<li><i>How</i> to draw, handled by {@link}.</li>
<p>For instance, {@link Canvas} provides a method to draw a line, while
{@link Paint} provides methods to define that line's color. {@link Canvas} has a
method to draw a rectangle, while {@link Paint} defines whether to fill that
rectangle with a
color or leave it empty. Simply put, {@link Canvas} defines shapes that you
can draw on the
screen, while {@link Paint} defines the color, style, font, and so forth of
each shape you
<p>So, before you draw anything, you need to create one or more {@link Paint}
objects. The {@code PieChart} example does this in a method called {@code init}, which is
called from the
private void init() {
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
if (mTextHeight == 0) {
mTextHeight = mTextPaint.getTextSize();
} else {
mPiePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mShadowPaint = new Paint(0);
mShadowPaint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));
<p>Creating objects ahead of time is an important optimization. Views are redrawn very frequently,
and many drawing
objects require expensive initialization. Creating drawing objects within your {@link
android.view.View#onDraw( onDraw()}
method significantly
reduces performance and can make your UI appear sluggish.</p>
<h2 id="layouteevent">Handle Layout Events</h2>
<p>In order to properly draw your custom view, you need to know what size it is. Complex custom
views often need to
perform multiple layout calculations depending on the size and shape of their area on screen. You
should never make
assumptions about the size of your view on the screen. Even if only one app uses your view, that app
needs to handle
different screen sizes, multiple screen densities, and various aspect ratios in both portrait and
landscape mode.</p>
<p>Although {@link android.view.View} has many methods for handling measurement, most of them do not
need to be
overridden. If your view doesn't need special control over its size, you only need to override one
method: {@link
android.view.View#onSizeChanged onSizeChanged()}.</p>
android.view.View#onSizeChanged onSizeChanged()} is called when your view is first assigned a size,
and again if the size of your view changes
for any reason. Calculate positions, dimensions, and any other values related to your view's size in
android.view.View#onSizeChanged onSizeChanged()}, instead of recalculating them every time you draw.
In the {@code PieChart} example, {@link
android.view.View#onSizeChanged onSizeChanged()} is
where the {@code PieChart} view calculates the bounding rectangle of the pie chart and the relative position
of the text label
and other visual elements.</p>
<p>When your view is assigned a size, the layout manager assumes that the size includes all of the
view's padding. You
must handle the padding values when you calculate your view's size. Here's a snippet from {@code
that shows how to do this:</p>
// Account for padding
float xpad = (float)(getPaddingLeft() + getPaddingRight());
float ypad = (float)(getPaddingTop() + getPaddingBottom());
// Account for the label
if (mShowText) xpad += mTextWidth;
float ww = (float)w - xpad;
float hh = (float)h - ypad;
// Figure out how big we can make the pie.
float diameter = Math.min(ww, hh);
<p>If you need finer control over your view's layout parameters, implement {@link
android.view.View#onMeasure onMeasure()}. This method's parameters are
{@link android.view.View.MeasureSpec} values that tell you how big your view's
parent wants your view to be, and whether that size is a hard maximum or just a suggestion. As an
optimization, these
values are stored as packed integers, and you use the static methods of
{@link android.view.View.MeasureSpec} to
unpack the information
stored in each integer.
<p>Here's an example implementation of {@link android.view.View#onMeasure onMeasure()}.
In this implementation, {@code PieChart}
attempts to make its area
big enough to make the pie as big as its label:</p>
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Try for a width based on our minimum
int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
int w = resolveSizeAndState(minw, widthMeasureSpec, 1);
// Whatever the width ends up being, ask for a height that would let the pie
// get as big as it can
int minh = MeasureSpec.getSize(w) - (int)mTextWidth + getPaddingBottom() + getPaddingTop();
int h = resolveSizeAndState(MeasureSpec.getSize(w) - (int)mTextWidth, heightMeasureSpec, 0);
setMeasuredDimension(w, h);
<p>There are three important things to note in this code:</p>
<li>The calculations take into account the view's padding. As mentioned earlier, this is the
<li>The helper method {@link android.view.View#resolveSizeAndState resolveSizeAndState()} is
used to create the
final width and height values. This helper returns an appropriate
{@link android.view.View.MeasureSpec} value
by comparing the view's desired size to the spec passed into
{@link android.view.View#onMeasure onMeasure()}.
<li>{@link android.view.View#onMeasure onMeasure()} has no return value.
Instead, the method communicates its results by
calling {@link
android.view.View#setMeasuredDimension setMeasuredDimension()}. Calling this method is
mandatory. If you omit
this call, the {@link android.view.View} class throws a runtime exception.
<h2 id="draw">Draw!</h2>
<p>Once you have your object creation and measuring code defined, you can implement {@link
android.view.View#onDraw( onDraw()}. Every view
implements {@link
android.view.View#onDraw( onDraw()}
differently, but there are some common operations that most views
<li>Draw text using {@link drawText()}. Specify the typeface by
calling {@link setTypeface()}, and the text color by calling {@link setColor()}.
<li>Draw primitive shapes using {@link drawRect()}, {@link drawOval()}, and {@link
drawArc()}. Change
whether the shapes are filled, outlined, or both by calling {@link setStyle()}.
<li>Draw more complex shapes using the {@link} class.
Define a shape by adding lines and curves to a
{@link} object, then draw the shape using {@link drawPath()}.
Just as with primitive shapes, paths can be outlined, filled, or both, depending on the
Define gradient fills by creating {@link} objects. Call {@link setShader()} to use your
{@link} on filled
<li>Draw bitmaps using {@link drawBitmap()}.</li>
<p>For example, here's the code that draws {@code PieChart}. It uses a mix of text, lines, and shapes.</p>
protected void onDraw(Canvas canvas) {
// Draw the shadow
// Draw the label text
canvas.drawText(mData.get(mCurrentItem).mLabel, mTextX, mTextY, mTextPaint);
// Draw the pie slices
for (int i = 0; i &lt; mData.size(); ++i) {
Item it = mData.get(i);
360 - it.mEndAngle,
it.mEndAngle - it.mStartAngle,
true, mPiePaint);
// Draw the pointer
canvas.drawLine(mTextX, mPointerY, mPointerX, mPointerY, mTextPaint);
canvas.drawCircle(mPointerX, mPointerY, mPointerSize, mTextPaint);