Home | History | Annotate | Download | only in drawable
      1 /*
      2  * Copyright (C) 2012 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.camera.drawable;
     18 
     19 import android.content.res.Resources;
     20 import android.graphics.Canvas;
     21 import android.graphics.Color;
     22 import android.graphics.ColorFilter;
     23 import android.graphics.Paint;
     24 import android.graphics.Typeface;
     25 import android.graphics.Paint.Align;
     26 import android.graphics.Rect;
     27 import android.graphics.drawable.Drawable;
     28 import android.util.TypedValue;
     29 
     30 
     31 public class TextDrawable extends Drawable {
     32 
     33     private static final int DEFAULT_COLOR = Color.WHITE;
     34     private static final int DEFAULT_TEXTSIZE = 15;
     35 
     36     private Paint mPaint;
     37     private CharSequence mText;
     38     private int mIntrinsicWidth;
     39     private int mIntrinsicHeight;
     40     private boolean mUseDropShadow;
     41 
     42     public TextDrawable(Resources res) {
     43         this(res, "");
     44     }
     45 
     46     public TextDrawable(Resources res, CharSequence text) {
     47         mText = text;
     48         updatePaint();
     49         float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
     50                 DEFAULT_TEXTSIZE, res.getDisplayMetrics());
     51         mPaint.setTextSize(textSize);
     52         mIntrinsicWidth = (int) (mPaint.measureText(mText, 0, mText.length()) + .5);
     53         mIntrinsicHeight = mPaint.getFontMetricsInt(null);
     54     }
     55 
     56     private void updatePaint() {
     57         if (mPaint == null) {
     58             mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
     59         }
     60         mPaint.setColor(DEFAULT_COLOR);
     61         mPaint.setTextAlign(Align.CENTER);
     62         if (mUseDropShadow) {
     63             mPaint.setTypeface(Typeface.DEFAULT_BOLD);
     64             mPaint.setShadowLayer(10, 0, 0, 0xff000000);
     65         } else {
     66             mPaint.setTypeface(Typeface.DEFAULT);
     67             mPaint.setShadowLayer(0, 0, 0, 0);
     68         }
     69     }
     70 
     71     public void setText(CharSequence txt) {
     72         mText = txt;
     73         if (txt == null) {
     74             mIntrinsicWidth = 0;
     75             mIntrinsicHeight = 0;
     76         } else {
     77             mIntrinsicWidth = (int) (mPaint.measureText(mText, 0, mText.length()) + .5);
     78             mIntrinsicHeight = mPaint.getFontMetricsInt(null);
     79         }
     80     }
     81 
     82     @Override
     83     public void draw(Canvas canvas) {
     84         if (mText != null) {
     85             Rect bounds = getBounds();
     86             canvas.drawText(mText, 0, mText.length(),
     87                     bounds.centerX(), bounds.centerY(), mPaint);
     88         }
     89     }
     90 
     91     public void setDropShadow(boolean shadow) {
     92         mUseDropShadow = shadow;
     93         updatePaint();
     94     }
     95 
     96     @Override
     97     public int getOpacity() {
     98         return mPaint.getAlpha();
     99     }
    100 
    101     @Override
    102     public int getIntrinsicWidth() {
    103         return mIntrinsicWidth;
    104     }
    105 
    106     @Override
    107     public int getIntrinsicHeight() {
    108         return mIntrinsicHeight;
    109     }
    110 
    111     @Override
    112     public void setAlpha(int alpha) {
    113         mPaint.setAlpha(alpha);
    114     }
    115 
    116     @Override
    117     public void setColorFilter(ColorFilter filter) {
    118         mPaint.setColorFilter(filter);
    119     }
    120 
    121 }
    122