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 17package com.android.deskclock; 18 19import android.content.Context; 20import android.graphics.Typeface; 21import android.util.AttributeSet; 22import android.widget.TextView; 23 24/** 25 * Displays text with no padding at the top. 26 */ 27public 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 38 private int mPaddingRight = 0; 39 40 public ZeroTopPaddingTextView(Context context) { 41 this(context, null); 42 } 43 44 public ZeroTopPaddingTextView(Context context, AttributeSet attrs) { 45 this(context, attrs, 0); 46 } 47 48 public ZeroTopPaddingTextView(Context context, AttributeSet attrs, int defStyle) { 49 super(context, attrs, defStyle); 50 setIncludeFontPadding(false); 51 updatePadding(); 52 } 53 54 public void updatePadding() { 55 float paddingRatio = NORMAL_FONT_PADDING_RATIO; 56 float bottomPaddingRatio = NORMAL_FONT_BOTTOM_PADDING_RATIO; 57 if (getTypeface().equals(SAN_SERIF_BOLD)) { 58 paddingRatio = BOLD_FONT_PADDING_RATIO; 59 bottomPaddingRatio = BOLD_FONT_BOTTOM_PADDING_RATIO; 60 } 61 // no need to scale by display density because getTextSize() already returns the font 62 // height in px 63 setPadding(0, (int) (-paddingRatio * getTextSize()), mPaddingRight, 64 (int) (-bottomPaddingRatio * getTextSize())); 65 } 66 67 public void setPaddingRight(int padding) { 68 mPaddingRight = padding; 69 updatePadding(); 70 } 71} 72