• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Paint.Align;
25 import android.graphics.Rect;
26 import android.graphics.drawable.Drawable;
27 import android.util.TypedValue;
28 
29 
30 public class TextDrawable extends Drawable {
31 
32     private static final int DEFAULT_COLOR = Color.WHITE;
33     private static final int DEFAULT_TEXTSIZE = 15;
34 
35     private Paint mPaint;
36     private CharSequence mText;
37     private int mIntrinsicWidth;
38     private int mIntrinsicHeight;
39 
TextDrawable(Resources res, CharSequence text)40     public TextDrawable(Resources res, CharSequence text) {
41         mText = text;
42         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
43         mPaint.setColor(DEFAULT_COLOR);
44         mPaint.setTextAlign(Align.CENTER);
45         float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
46                 DEFAULT_TEXTSIZE, res.getDisplayMetrics());
47         mPaint.setTextSize(textSize);
48         mIntrinsicWidth = (int) (mPaint.measureText(mText, 0, mText.length()) + .5);
49         mIntrinsicHeight = mPaint.getFontMetricsInt(null);
50     }
51 
52     @Override
draw(Canvas canvas)53     public void draw(Canvas canvas) {
54         Rect bounds = getBounds();
55         canvas.drawText(mText, 0, mText.length(),
56                 bounds.centerX(), bounds.centerY(), mPaint);
57     }
58 
59     @Override
getOpacity()60     public int getOpacity() {
61         return mPaint.getAlpha();
62     }
63 
64     @Override
getIntrinsicWidth()65     public int getIntrinsicWidth() {
66         return mIntrinsicWidth;
67     }
68 
69     @Override
getIntrinsicHeight()70     public int getIntrinsicHeight() {
71         return mIntrinsicHeight;
72     }
73 
74     @Override
setAlpha(int alpha)75     public void setAlpha(int alpha) {
76         mPaint.setAlpha(alpha);
77     }
78 
79     @Override
setColorFilter(ColorFilter filter)80     public void setColorFilter(ColorFilter filter) {
81         mPaint.setColorFilter(filter);
82     }
83 
84 }
85