• 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.deskclock;
18 
19 import android.content.Context;
20 import android.graphics.Typeface;
21 import android.util.AttributeSet;
22 import android.widget.TextView;
23 
24 /**
25  * Displays text with no padding at the top.
26  */
27 public class ZeroTopPaddingTextView extends TextView {
28     private static final float NORMAL_FONT_PADDING_RATIO = 0.328f;
29     // the bold fontface has less empty space on the top
30     private static final float BOLD_FONT_PADDING_RATIO = 0.208f;
31 
32     private static final float NORMAL_FONT_BOTTOM_PADDING_RATIO = 0.25f;
33     // the bold fontface has less empty space on the top
34     private static final float BOLD_FONT_BOTTOM_PADDING_RATIO = 0.208f;
35 
36     private static final Typeface SAN_SERIF_BOLD = Typeface.create("san-serif", Typeface.BOLD);
37     private static final Typeface SAN_SERIF__CONDENSED_BOLD =
38             Typeface.create("sans-serif-condensed", Typeface.BOLD);
39 
40     private int mPaddingRight = 0;
41 
ZeroTopPaddingTextView(Context context)42     public ZeroTopPaddingTextView(Context context) {
43         this(context, null);
44     }
45 
ZeroTopPaddingTextView(Context context, AttributeSet attrs)46     public ZeroTopPaddingTextView(Context context, AttributeSet attrs) {
47         this(context, attrs, 0);
48     }
49 
ZeroTopPaddingTextView(Context context, AttributeSet attrs, int defStyle)50     public ZeroTopPaddingTextView(Context context, AttributeSet attrs, int defStyle) {
51         super(context, attrs, defStyle);
52         setIncludeFontPadding(false);
53         updatePadding();
54     }
55 
updatePadding()56     public void updatePadding() {
57         float paddingRatio = NORMAL_FONT_PADDING_RATIO;
58         float bottomPaddingRatio = NORMAL_FONT_BOTTOM_PADDING_RATIO;
59         if (getTypeface().equals(SAN_SERIF_BOLD) ||
60                     getTypeface().equals(SAN_SERIF__CONDENSED_BOLD)) {
61             paddingRatio = BOLD_FONT_PADDING_RATIO;
62             bottomPaddingRatio = BOLD_FONT_BOTTOM_PADDING_RATIO;
63         }
64         // no need to scale by display density because getTextSize() already returns the font
65         // height in px
66         setPadding(0, (int) (-paddingRatio * getTextSize()), mPaddingRight,
67                 (int) (-bottomPaddingRatio * getTextSize()));
68     }
69 
setPaddingRight(int padding)70     public void setPaddingRight(int padding) {
71         mPaddingRight = padding;
72         updatePadding();
73     }
74 }
75