blob: 9607195cded01d1680283f2faf1fff7d66856d6e [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2014 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.dialer.dialpadview;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Paint;
22import android.graphics.Rect;
23import android.util.AttributeSet;
24import android.widget.TextView;
25
26/**
linyuhbd036002017-10-31 10:29:45 -070027 * This is a custom text view intended for rendering text on the dialpad. TextView has built-in
28 * top/bottom padding to help account for ascenders/descenders.
Eric Erfanianccca3152017-02-22 16:32:36 -080029 *
30 * <p>Since vertical space is at a premium on the dialpad, particularly if the font size is scaled
31 * to a larger default, for the dialpad we use this class to more precisely render characters
32 * according to the precise amount of space they need.
33 */
34public class DialpadTextView extends TextView {
35
linyuh183cb712017-12-27 17:02:37 -080036 private Rect textBounds = new Rect();
37 private String textStr;
Eric Erfanianccca3152017-02-22 16:32:36 -080038
39 public DialpadTextView(Context context, AttributeSet attrs) {
40 super(context, attrs);
41 }
42
43 /** Draw the text to fit within the height/width which have been specified during measurement. */
44 @Override
45 public void draw(Canvas canvas) {
46 Paint paint = getPaint();
47
48 // Without this, the draw does not respect the style's specified text color.
49 paint.setColor(getCurrentTextColor());
50
51 // The text bounds values are relative and can be negative,, so rather than specifying a
52 // standard origin such as 0, 0, we need to use negative of the left/top bounds.
53 // For example, the bounds may be: Left: 11, Right: 37, Top: -77, Bottom: 0
linyuh183cb712017-12-27 17:02:37 -080054 canvas.drawText(textStr, -textBounds.left, -textBounds.top, paint);
Eric Erfanianccca3152017-02-22 16:32:36 -080055 }
56
57 /**
58 * Calculate the pixel-accurate bounds of the text when rendered, and use that to specify the
59 * height and width.
60 */
61 @Override
62 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
63 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
linyuh183cb712017-12-27 17:02:37 -080064 textStr = getText().toString();
65 getPaint().getTextBounds(textStr, 0, textStr.length(), textBounds);
Eric Erfanianccca3152017-02-22 16:32:36 -080066
linyuh183cb712017-12-27 17:02:37 -080067 int width = resolveSize(textBounds.width(), widthMeasureSpec);
68 int height = resolveSize(textBounds.height(), heightMeasureSpec);
Eric Erfanianccca3152017-02-22 16:32:36 -080069 setMeasuredDimension(width, height);
70 }
71}