1 package ${packageName};
3 import android.content.Context;
4 import android.content.res.TypedArray;
5 import android.graphics.Canvas;
6 import android.graphics.Color;
7 import android.graphics.Paint;
8 import android.graphics.drawable.Drawable;
9 import android.text.TextPaint;
10 import android.util.AttributeSet;
11 import android.view.View;
12 <#if applicationPackage??>import ${applicationPackage}.R;</#if>
15 * TODO: document your custom view class.
17 public class ${viewClass} extends View {
18 private String mExampleString; // TODO: use a default from R.string...
19 private int mExampleColor = Color.RED; // TODO: use a default from R.color...
20 private float mExampleDimension = 0; // TODO: use a default from R.dimen...
21 private Drawable mExampleDrawable;
23 private TextPaint mTextPaint;
24 private float mTextWidth;
25 private float mTextHeight;
27 public ${viewClass}(Context context) {
32 public ${viewClass}(Context context, AttributeSet attrs) {
33 super(context, attrs);
37 public ${viewClass}(Context context, AttributeSet attrs, int defStyle) {
38 super(context, attrs, defStyle);
39 init(attrs, defStyle);
42 private void init(AttributeSet attrs, int defStyle) {
44 final TypedArray a = getContext().obtainStyledAttributes(
45 attrs, R.styleable.${viewClass}, defStyle, 0);
47 mExampleString = a.getString(
48 R.styleable.${viewClass}_exampleString);
49 mExampleColor = a.getColor(
50 R.styleable.${viewClass}_exampleColor,
52 // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
53 // values that should fall on pixel boundaries.
54 mExampleDimension = a.getDimension(
55 R.styleable.${viewClass}_exampleDimension,
58 if (a.hasValue(R.styleable.${viewClass}_exampleDrawable)) {
59 mExampleDrawable = a.getDrawable(
60 R.styleable.${viewClass}_exampleDrawable);
61 mExampleDrawable.setCallback(this);
66 // Set up a default TextPaint object
67 mTextPaint = new TextPaint();
68 mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
69 mTextPaint.setTextAlign(Paint.Align.LEFT);
71 // Update TextPaint and text measurements from attributes
72 invalidateTextPaintAndMeasurements();
75 private void invalidateTextPaintAndMeasurements() {
76 mTextPaint.setTextSize(mExampleDimension);
77 mTextPaint.setColor(mExampleColor);
78 mTextWidth = mTextPaint.measureText(mExampleString);
80 Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
81 mTextHeight = fontMetrics.bottom;
85 protected void onDraw(Canvas canvas) {
88 // TODO: consider storing these as member variables to reduce
89 // allocations per draw cycle.
90 int paddingLeft = getPaddingLeft();
91 int paddingTop = getPaddingTop();
92 int paddingRight = getPaddingRight();
93 int paddingBottom = getPaddingBottom();
95 int contentWidth = getWidth() - paddingLeft - paddingRight;
96 int contentHeight = getHeight() - paddingTop - paddingBottom;
99 canvas.drawText(mExampleString,
100 paddingLeft + (contentWidth - mTextWidth) / 2,
101 paddingTop + (contentHeight + mTextHeight) / 2,
104 // Draw the example drawable on top of the text.
105 if (mExampleDrawable != null) {
106 mExampleDrawable.setBounds(paddingLeft, paddingTop,
107 paddingLeft + contentWidth, paddingTop + contentHeight);
108 mExampleDrawable.draw(canvas);
113 * Gets the example string attribute value.
114 * @return The example string attribute value.
116 public String getExampleString() {
117 return mExampleString;
121 * Sets the view's example string attribute value. In the example view, this string
122 * is the text to draw.
123 * @param exampleString The example string attribute value to use.
125 public void setExampleString(String exampleString) {
126 mExampleString = exampleString;
127 invalidateTextPaintAndMeasurements();
131 * Gets the example color attribute value.
132 * @return The example color attribute value.
134 public int getExampleColor() {
135 return mExampleColor;
139 * Sets the view's example color attribute value. In the example view, this color
141 * @param exampleColor The example color attribute value to use.
143 public void setExampleColor(int exampleColor) {
144 mExampleColor = exampleColor;
145 invalidateTextPaintAndMeasurements();
149 * Gets the example dimension attribute value.
150 * @return The example dimension attribute value.
152 public float getExampleDimension() {
153 return mExampleDimension;
157 * Sets the view's example dimension attribute value. In the example view, this dimension
159 * @param exampleDimension The example dimension attribute value to use.
161 public void setExampleDimension(float exampleDimension) {
162 mExampleDimension = exampleDimension;
163 invalidateTextPaintAndMeasurements();
167 * Gets the example drawable attribute value.
168 * @return The example drawable attribute value.
170 public Drawable getExampleDrawable() {
171 return mExampleDrawable;
175 * Sets the view's example drawable attribute value. In the example view, this drawable is
176 * drawn above the text.
177 * @param exampleDrawable The example drawable attribute value to use.
179 public void setExampleDrawable(Drawable exampleDrawable) {
180 mExampleDrawable = exampleDrawable;