diff options
author | Tor Norbye <tnorbye@google.com> | 2012-05-25 17:05:55 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-06-04 16:57:34 -0700 |
commit | 56afaff9c34a60c0e850324938d101a0509f8a38 (patch) | |
tree | 8a945bf07d4cb162d2c1b0c1d13989ccaa55da41 /templates/other/CustomView/root | |
parent | 6de0364094c5780cf39f4805ac507374c382da2f (diff) | |
download | sdk-56afaff9c34a60c0e850324938d101a0509f8a38.zip sdk-56afaff9c34a60c0e850324938d101a0509f8a38.tar.gz sdk-56afaff9c34a60c0e850324938d101a0509f8a38.tar.bz2 |
Allow project templates to be bundled separately
This changeset moves the various FreeMarker templates out of the ADT
plugin and over into the tools install area. The code to instantiate
templates is simplified a bit now that it no longer needs to both
handle files on disk and files read out of the .jar.
There's a new first page to the various template wizards which is
shown if the tools are not up to date (specifically, if the
$SDK/tools/templates/ directory does not exist). This page explains
that the tools have to be updated, and the Next button is disabled
until they are up to date.
This CL also contains some other tweaks suggested by Roman to the
activity-to-layout name mapping and misc code changes.
Change-Id: I3bc65f54a6b79bbeedfb917a9d34ec0d312f3526
Diffstat (limited to 'templates/other/CustomView/root')
3 files changed, 207 insertions, 0 deletions
diff --git a/templates/other/CustomView/root/res/layout/sample.xml.ftl b/templates/other/CustomView/root/res/layout/sample.xml.ftl new file mode 100755 index 0000000..bdd8c8b --- /dev/null +++ b/templates/other/CustomView/root/res/layout/sample.xml.ftl @@ -0,0 +1,18 @@ +<FrameLayout + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res/${packageName}" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <${packageName}.${viewClass} + android:background="#ccc" + android:layout_width="300dp" + android:layout_height="300dp" + android:paddingLeft="20dp" + android:paddingBottom="40dp" + app:exampleDimension="24sp" + app:exampleColor="#33b5e5" + app:exampleString="Hello, ${viewClass}" + app:exampleDrawable="@android:drawable/ic_menu_add" /> + +</FrameLayout>
\ No newline at end of file diff --git a/templates/other/CustomView/root/res/values/attrs.xml.ftl b/templates/other/CustomView/root/res/values/attrs.xml.ftl new file mode 100755 index 0000000..89059d2 --- /dev/null +++ b/templates/other/CustomView/root/res/values/attrs.xml.ftl @@ -0,0 +1,8 @@ +<resources> + <declare-styleable name="${viewClass}"> + <attr name="exampleString" format="string" /> + <attr name="exampleDimension" format="dimension" /> + <attr name="exampleColor" format="color" /> + <attr name="exampleDrawable" format="color|reference" /> + </declare-styleable> +</resources> diff --git a/templates/other/CustomView/root/src/app_package/CustomView.java.ftl b/templates/other/CustomView/root/src/app_package/CustomView.java.ftl new file mode 100644 index 0000000..e1c7e13 --- /dev/null +++ b/templates/other/CustomView/root/src/app_package/CustomView.java.ftl @@ -0,0 +1,181 @@ +package ${packageName}; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.drawable.Drawable; +import android.text.TextPaint; +import android.util.AttributeSet; +import android.view.View; + +/** + * TODO: document your custom view class. + */ +public class ${viewClass} extends View { + private String mExampleString; // TODO: use a default from R.string... + private int mExampleColor = Color.RED; // TODO: use a default from R.color... + private float mExampleDimension = 0; // TODO: use a default from R.dimen... + private Drawable mExampleDrawable; + + private TextPaint mTextPaint; + private float mTextWidth; + private float mTextHeight; + + public ${viewClass}(Context context) { + super(context); + init(null, 0); + } + + public ${viewClass}(Context context, AttributeSet attrs) { + super(context, attrs); + init(attrs, 0); + } + + public ${viewClass}(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(attrs, defStyle); + } + + private void init(AttributeSet attrs, int defStyle) { + // Load attributes + final TypedArray a = getContext().obtainStyledAttributes( + attrs, R.styleable.${viewClass}, defStyle, 0); + + mExampleString = a.getString( + R.styleable.${viewClass}_exampleString); + mExampleColor = a.getColor( + R.styleable.${viewClass}_exampleColor, + mExampleColor); + // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with + // values that should fall on pixel boundaries. + mExampleDimension = a.getDimension( + R.styleable.${viewClass}_exampleDimension, + mExampleDimension); + + if (a.hasValue(R.styleable.${viewClass}_exampleDrawable)) { + mExampleDrawable = a.getDrawable( + R.styleable.${viewClass}_exampleDrawable); + mExampleDrawable.setCallback(this); + } + + a.recycle(); + + // Set up a default TextPaint object + mTextPaint = new TextPaint(); + mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG); + mTextPaint.setTextAlign(Paint.Align.LEFT); + + // Update TextPaint and text measurements from attributes + invalidateTextPaintAndMeasurements(); + } + + private void invalidateTextPaintAndMeasurements() { + mTextPaint.setTextSize(mExampleDimension); + mTextPaint.setColor(mExampleColor); + mTextWidth = mTextPaint.measureText(mExampleString); + + Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics(); + mTextHeight = fontMetrics.bottom; + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + // TODO: consider storing these as member variables to reduce + // allocations per draw cycle. + int paddingLeft = getPaddingLeft(); + int paddingTop = getPaddingTop(); + int paddingRight = getPaddingRight(); + int paddingBottom = getPaddingBottom(); + + int contentWidth = getWidth() - paddingLeft - paddingRight; + int contentHeight = getHeight() - paddingTop - paddingBottom; + + // Draw the text. + canvas.drawText(mExampleString, + paddingLeft + (contentWidth - mTextWidth) / 2, + paddingTop + (contentHeight + mTextHeight) / 2, + mTextPaint); + + // Draw the example drawable on top of the text. + if (mExampleDrawable != null) { + mExampleDrawable.setBounds(paddingLeft, paddingTop, + paddingLeft + contentWidth, paddingTop + contentHeight); + mExampleDrawable.draw(canvas); + } + } + + /** + * Gets the example string attribute value. + * @return The example string attribute value. + */ + public String getExampleString() { + return mExampleString; + } + + /** + * Sets the view's example string attribute value. In the example view, this string + * is the text to draw. + * @param exampleString The example string attribute value to use. + */ + public void setExampleString(String exampleString) { + mExampleString = exampleString; + invalidateTextPaintAndMeasurements(); + } + + /** + * Gets the example color attribute value. + * @return The example color attribute value. + */ + public int getExampleColor() { + return mExampleColor; + } + + /** + * Sets the view's example color attribute value. In the example view, this color + * is the font color. + * @param exampleColor The example color attribute value to use. + */ + public void setExampleColor(int exampleColor) { + mExampleColor = exampleColor; + invalidateTextPaintAndMeasurements(); + } + + /** + * Gets the example dimension attribute value. + * @return The example dimension attribute value. + */ + public float getExampleDimension() { + return mExampleDimension; + } + + /** + * Sets the view's example dimension attribute value. In the example view, this dimension + * is the font size. + * @param exampleDimension The example dimension attribute value to use. + */ + public void setExampleDimension(float exampleDimension) { + mExampleDimension = exampleDimension; + invalidateTextPaintAndMeasurements(); + } + + /** + * Gets the example drawable attribute value. + * @return The example drawable attribute value. + */ + public Drawable getExampleDrawable() { + return mExampleDrawable; + } + + /** + * Sets the view's example drawable attribute value. In the example view, this drawable is + * drawn above the text. + * @param exampleDrawable The example drawable attribute value to use. + */ + public void setExampleDrawable(Drawable exampleDrawable) { + mExampleDrawable = exampleDrawable; + } +} |