Debian Codegraph @

templates/other/CustomView/root/src/app_package/CustomView.java.ftl

raw
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??>
13 import ${applicationPackage}.R;
14 </#if>
15
16 /**
17 * TODO: document your custom view class.
18 */
19 public class ${viewClass} extends View {
20 private String mExampleString; // TODO: use a default from R.string...
21 private int mExampleColor = Color.RED; // TODO: use a default from R.color...
22 private float mExampleDimension = 0; // TODO: use a default from R.dimen...
23 private Drawable mExampleDrawable;
24
25 private TextPaint mTextPaint;
26 private float mTextWidth;
27 private float mTextHeight;
28
29 public ${viewClass}(Context context) {
30 super(context);
31 init(null, 0);
32 }
33
34 public ${viewClass}(Context context, AttributeSet attrs) {
35 super(context, attrs);
36 init(attrs, 0);
37 }
38
39 public ${viewClass}(Context context, AttributeSet attrs, int defStyle) {
40 super(context, attrs, defStyle);
41 init(attrs, defStyle);
42 }
43
44 private void init(AttributeSet attrs, int defStyle) {
45 // Load attributes
46 final TypedArray a = getContext().obtainStyledAttributes(
47 attrs, R.styleable.${viewClass}, defStyle, 0);
48
49 mExampleString = a.getString(
50 R.styleable.${viewClass}_exampleString);
51 mExampleColor = a.getColor(
52 R.styleable.${viewClass}_exampleColor,
53 mExampleColor);
54 // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
55 // values that should fall on pixel boundaries.
56 mExampleDimension = a.getDimension(
57 R.styleable.${viewClass}_exampleDimension,
58 mExampleDimension);
59
60 if (a.hasValue(R.styleable.${viewClass}_exampleDrawable)) {
61 mExampleDrawable = a.getDrawable(
62 R.styleable.${viewClass}_exampleDrawable);
63 mExampleDrawable.setCallback(this);
64 }
65
66 a.recycle();
67
68 // Set up a default TextPaint object
69 mTextPaint = new TextPaint();
70 mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
71 mTextPaint.setTextAlign(Paint.Align.LEFT);
72
73 // Update TextPaint and text measurements from attributes
74 invalidateTextPaintAndMeasurements();
75 }
76
77 private void invalidateTextPaintAndMeasurements() {
78 mTextPaint.setTextSize(mExampleDimension);
79 mTextPaint.setColor(mExampleColor);
80 mTextWidth = mTextPaint.measureText(mExampleString);
81
82 Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
83 mTextHeight = fontMetrics.bottom;
84 }
85
86 @Override
87 protected void onDraw(Canvas canvas) {
88 super.onDraw(canvas);
89
90 // TODO: consider storing these as member variables to reduce
91 // allocations per draw cycle.
92 int paddingLeft = getPaddingLeft();
93 int paddingTop = getPaddingTop();
94 int paddingRight = getPaddingRight();
95 int paddingBottom = getPaddingBottom();
96
97 int contentWidth = getWidth() - paddingLeft - paddingRight;
98 int contentHeight = getHeight() - paddingTop - paddingBottom;
99
100 // Draw the text.
101 canvas.drawText(mExampleString,
102 paddingLeft + (contentWidth - mTextWidth) / 2,
103 paddingTop + (contentHeight + mTextHeight) / 2,
104 mTextPaint);
105
106 // Draw the example drawable on top of the text.
107 if (mExampleDrawable != null) {
108 mExampleDrawable.setBounds(paddingLeft, paddingTop,
109 paddingLeft + contentWidth, paddingTop + contentHeight);
110 mExampleDrawable.draw(canvas);
111 }
112 }
113
114 /**
115 * Gets the example string attribute value.
116 * @return The example string attribute value.
117 */
118 public String getExampleString() {
119 return mExampleString;
120 }
121
122 /**
123 * Sets the view's example string attribute value. In the example view, this string
124 * is the text to draw.
125 * @param exampleString The example string attribute value to use.
126 */
127 public void setExampleString(String exampleString) {
128 mExampleString = exampleString;
129 invalidateTextPaintAndMeasurements();
130 }
131
132 /**
133 * Gets the example color attribute value.
134 * @return The example color attribute value.
135 */
136 public int getExampleColor() {
137 return mExampleColor;
138 }
139
140 /**
141 * Sets the view's example color attribute value. In the example view, this color
142 * is the font color.
143 * @param exampleColor The example color attribute value to use.
144 */
145 public void setExampleColor(int exampleColor) {
146 mExampleColor = exampleColor;
147 invalidateTextPaintAndMeasurements();
148 }
149
150 /**
151 * Gets the example dimension attribute value.
152 * @return The example dimension attribute value.
153 */
154 public float getExampleDimension() {
155 return mExampleDimension;
156 }
157
158 /**
159 * Sets the view's example dimension attribute value. In the example view, this dimension
160 * is the font size.
161 * @param exampleDimension The example dimension attribute value to use.
162 */
163 public void setExampleDimension(float exampleDimension) {
164 mExampleDimension = exampleDimension;
165 invalidateTextPaintAndMeasurements();
166 }
167
168 /**
169 * Gets the example drawable attribute value.
170 * @return The example drawable attribute value.
171 */
172 public Drawable getExampleDrawable() {
173 return mExampleDrawable;
174 }
175
176 /**
177 * Sets the view's example drawable attribute value. In the example view, this drawable is
178 * drawn above the text.
179 * @param exampleDrawable The example drawable attribute value to use.
180 */
181 public void setExampleDrawable(Drawable exampleDrawable) {
182 mExampleDrawable = exampleDrawable;
183 }
184 }