Home | History | Annotate | Download | only in launcher2
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.launcher2;
     18 
     19 import android.content.Context;
     20 import android.content.res.TypedArray;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Canvas;
     23 import android.graphics.Paint;
     24 import android.graphics.PorterDuff;
     25 import android.graphics.Rect;
     26 import android.graphics.drawable.Drawable;
     27 import android.util.AttributeSet;
     28 import android.widget.TextView;
     29 
     30 import com.android.launcher.R;
     31 
     32 /**
     33  * This class adds a stroke to the generic TextView allowing the text to stand out better against
     34  * the background (ie. in the AllApps button).
     35  */
     36 public class StrokedTextView extends TextView {
     37     private final Canvas mCanvas = new Canvas();
     38     private final Paint mPaint = new Paint();
     39     private Bitmap mCache;
     40     private boolean mUpdateCachedBitmap;
     41     private int mStrokeColor;
     42     private float mStrokeWidth;
     43     private int mTextColor;
     44 
     45     public StrokedTextView(Context context) {
     46         super(context);
     47         init(context, null, 0);
     48     }
     49 
     50     public StrokedTextView(Context context, AttributeSet attrs) {
     51         super(context, attrs);
     52         init(context, attrs, 0);
     53     }
     54 
     55     public StrokedTextView(Context context, AttributeSet attrs, int defStyle) {
     56         super(context, attrs, defStyle);
     57         init(context, attrs, defStyle);
     58     }
     59 
     60     private void init(Context context, AttributeSet attrs, int defStyle) {
     61         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StrokedTextView,
     62                 defStyle, 0);
     63         mStrokeColor = a.getColor(R.styleable.StrokedTextView_strokeColor, 0xFF000000);
     64         mStrokeWidth = a.getFloat(R.styleable.StrokedTextView_strokeWidth, 0.0f);
     65         mTextColor = a.getColor(R.styleable.StrokedTextView_strokeTextColor, 0xFFFFFFFF);
     66         a.recycle();
     67         mUpdateCachedBitmap = true;
     68 
     69         // Setup the text paint
     70         mPaint.setAntiAlias(true);
     71         mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
     72     }
     73 
     74     protected void onTextChanged(CharSequence text, int start, int before, int after) {
     75         super.onTextChanged(text, start, before, after);
     76         mUpdateCachedBitmap = true;
     77     }
     78 
     79     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
     80         super.onSizeChanged(w, h, oldw, oldh);
     81         if (w > 0 && h > 0) {
     82             mUpdateCachedBitmap = true;
     83             mCache = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
     84         } else {
     85             mCache = null;
     86         }
     87     }
     88 
     89     protected void onDraw(Canvas canvas) {
     90         if (mCache != null) {
     91             if (mUpdateCachedBitmap) {
     92                 final int w = getMeasuredWidth();
     93                 final int h = getMeasuredHeight();
     94                 final String text = getText().toString();
     95                 final Rect textBounds = new Rect();
     96                 final Paint textPaint = getPaint();
     97                 final int textWidth = (int) textPaint.measureText(text);
     98                 textPaint.getTextBounds("x", 0, 1, textBounds);
     99 
    100                 // Clear the old cached image
    101                 mCanvas.setBitmap(mCache);
    102                 mCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
    103 
    104                 // Draw the drawable
    105                 final int drawableLeft = getPaddingLeft();
    106                 final int drawableTop = getPaddingTop();
    107                 final Drawable[] drawables = getCompoundDrawables();
    108                 for (int i = 0; i < drawables.length; ++i) {
    109                     if (drawables[i] != null) {
    110                         drawables[i].setBounds(drawableLeft, drawableTop,
    111                                 drawableLeft + drawables[i].getIntrinsicWidth(),
    112                                 drawableTop + drawables[i].getIntrinsicHeight());
    113                         drawables[i].draw(mCanvas);
    114                     }
    115                 }
    116 
    117                 final int left = w - getPaddingRight() - textWidth;
    118                 final int bottom = (h + textBounds.height()) / 2;
    119 
    120                 // Draw the outline of the text
    121                 mPaint.setStrokeWidth(mStrokeWidth);
    122                 mPaint.setColor(mStrokeColor);
    123                 mPaint.setTextSize(getTextSize());
    124                 mCanvas.drawText(text, left, bottom, mPaint);
    125 
    126                 // Draw the text itself
    127                 mPaint.setStrokeWidth(0);
    128                 mPaint.setColor(mTextColor);
    129                 mCanvas.drawText(text, left, bottom, mPaint);
    130 
    131                 mUpdateCachedBitmap = false;
    132             }
    133             canvas.drawBitmap(mCache, 0, 0, mPaint);
    134         } else {
    135             super.onDraw(canvas);
    136         }
    137     }
    138 }
    139