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