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 17package com.android.launcher2; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.graphics.Bitmap; 22import android.graphics.Canvas; 23import android.graphics.Paint; 24import android.graphics.PorterDuff; 25import android.graphics.Rect; 26import android.graphics.drawable.Drawable; 27import android.util.AttributeSet; 28import android.widget.TextView; 29 30import 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 */ 36public 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 gap = getCompoundDrawablePadding(); 93 final int w = getMeasuredWidth(); 94 final int h = getMeasuredHeight(); 95 final String text = getText().toString(); 96 final Rect textBounds = new Rect(); 97 final Paint textPaint = getPaint(); 98 final int textWidth = (int) textPaint.measureText(text); 99 textPaint.getTextBounds("x", 0, 1, textBounds); 100 101 // Clear the old cached image 102 mCanvas.setBitmap(mCache); 103 mCanvas.drawColor(0, PorterDuff.Mode.CLEAR); 104 105 // Draw the drawable 106 final int drawableLeft = getPaddingLeft(); 107 final int drawableTop = getPaddingTop(); 108 final Drawable[] drawables = getCompoundDrawables(); 109 for (int i = 0; i < drawables.length; ++i) { 110 if (drawables[i] != null) { 111 drawables[i].setBounds(drawableLeft, drawableTop, 112 drawableLeft + drawables[i].getIntrinsicWidth(), 113 drawableTop + drawables[i].getIntrinsicHeight()); 114 drawables[i].draw(mCanvas); 115 } 116 } 117 118 final int left = w - getPaddingRight() - textWidth; 119 final int bottom = (h + textBounds.height()) / 2; 120 121 // Draw the outline of the text 122 mPaint.setStrokeWidth(mStrokeWidth); 123 mPaint.setColor(mStrokeColor); 124 mPaint.setTextSize(getTextSize()); 125 mCanvas.drawText(text, left, bottom, mPaint); 126 127 // Draw the text itself 128 mPaint.setStrokeWidth(0); 129 mPaint.setColor(mTextColor); 130 mCanvas.drawText(text, left, bottom, mPaint); 131 132 mUpdateCachedBitmap = false; 133 } 134 canvas.drawBitmap(mCache, 0, 0, mPaint); 135 } else { 136 super.onDraw(canvas); 137 } 138 } 139} 140