| 1 |
package ${packageName}; |
| 2 |
|
| 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> |
| 13 |
|
| 14 |
/** |
| 15 |
* TODO: document your custom view class. |
| 16 |
*/ |
| 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; |
| 22 |
|
| 23 |
private TextPaint mTextPaint; |
| 24 |
private float mTextWidth; |
| 25 |
private float mTextHeight; |
| 26 |
|
| 27 |
public ${viewClass}(Context context) { |
| 28 |
super(context); |
| 29 |
init(null, 0); |
| 30 |
} |
| 31 |
|
| 32 |
public ${viewClass}(Context context, AttributeSet attrs) { |
| 33 |
super(context, attrs); |
| 34 |
init(attrs, 0); |
| 35 |
} |
| 36 |
|
| 37 |
public ${viewClass}(Context context, AttributeSet attrs, int defStyle) { |
| 38 |
super(context, attrs, defStyle); |
| 39 |
init(attrs, defStyle); |
| 40 |
} |
| 41 |
|
| 42 |
private void init(AttributeSet attrs, int defStyle) { |
| 43 |
// Load attributes |
| 44 |
final TypedArray a = getContext().obtainStyledAttributes( |
| 45 |
attrs, R.styleable.${viewClass}, defStyle, 0); |
| 46 |
|
| 47 |
mExampleString = a.getString( |
| 48 |
R.styleable.${viewClass}_exampleString); |
| 49 |
mExampleColor = a.getColor( |
| 50 |
R.styleable.${viewClass}_exampleColor, |
| 51 |
mExampleColor); |
| 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, |
| 56 |
mExampleDimension); |
| 57 |
|
| 58 |
if (a.hasValue(R.styleable.${viewClass}_exampleDrawable)) { |
| 59 |
mExampleDrawable = a.getDrawable( |
| 60 |
R.styleable.${viewClass}_exampleDrawable); |
| 61 |
mExampleDrawable.setCallback(this); |
| 62 |
} |
| 63 |
|
| 64 |
a.recycle(); |
| 65 |
|
| 66 |
// Set up a default TextPaint object |
| 67 |
mTextPaint = new TextPaint(); |
| 68 |
mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG); |
| 69 |
mTextPaint.setTextAlign(Paint.Align.LEFT); |
| 70 |
|
| 71 |
// Update TextPaint and text measurements from attributes |
| 72 |
invalidateTextPaintAndMeasurements(); |
| 73 |
} |
| 74 |
|
| 75 |
private void invalidateTextPaintAndMeasurements() { |
| 76 |
mTextPaint.setTextSize(mExampleDimension); |
| 77 |
mTextPaint.setColor(mExampleColor); |
| 78 |
mTextWidth = mTextPaint.measureText(mExampleString); |
| 79 |
|
| 80 |
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics(); |
| 81 |
mTextHeight = fontMetrics.bottom; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
protected void onDraw(Canvas canvas) { |
| 86 |
super.onDraw(canvas); |
| 87 |
|
| 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(); |
| 94 |
|
| 95 |
int contentWidth = getWidth() - paddingLeft - paddingRight; |
| 96 |
int contentHeight = getHeight() - paddingTop - paddingBottom; |
| 97 |
|
| 98 |
// Draw the text. |
| 99 |
canvas.drawText(mExampleString, |
| 100 |
paddingLeft + (contentWidth - mTextWidth) / 2, |
| 101 |
paddingTop + (contentHeight + mTextHeight) / 2, |
| 102 |
mTextPaint); |
| 103 |
|
| 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); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Gets the example string attribute value. |
| 114 |
* @return The example string attribute value. |
| 115 |
*/ |
| 116 |
public String getExampleString() { |
| 117 |
return mExampleString; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 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. |
| 124 |
*/ |
| 125 |
public void setExampleString(String exampleString) { |
| 126 |
mExampleString = exampleString; |
| 127 |
invalidateTextPaintAndMeasurements(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Gets the example color attribute value. |
| 132 |
* @return The example color attribute value. |
| 133 |
*/ |
| 134 |
public int getExampleColor() { |
| 135 |
return mExampleColor; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Sets the view's example color attribute value. In the example view, this color |
| 140 |
* is the font color. |
| 141 |
* @param exampleColor The example color attribute value to use. |
| 142 |
*/ |
| 143 |
public void setExampleColor(int exampleColor) { |
| 144 |
mExampleColor = exampleColor; |
| 145 |
invalidateTextPaintAndMeasurements(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Gets the example dimension attribute value. |
| 150 |
* @return The example dimension attribute value. |
| 151 |
*/ |
| 152 |
public float getExampleDimension() { |
| 153 |
return mExampleDimension; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Sets the view's example dimension attribute value. In the example view, this dimension |
| 158 |
* is the font size. |
| 159 |
* @param exampleDimension The example dimension attribute value to use. |
| 160 |
*/ |
| 161 |
public void setExampleDimension(float exampleDimension) { |
| 162 |
mExampleDimension = exampleDimension; |
| 163 |
invalidateTextPaintAndMeasurements(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Gets the example drawable attribute value. |
| 168 |
* @return The example drawable attribute value. |
| 169 |
*/ |
| 170 |
public Drawable getExampleDrawable() { |
| 171 |
return mExampleDrawable; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 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. |
| 178 |
*/ |
| 179 |
public void setExampleDrawable(Drawable exampleDrawable) { |
| 180 |
mExampleDrawable = exampleDrawable; |
| 181 |
} |
| 182 |
} |