Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 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.calculator2; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Paint; |
| 21 | import android.graphics.Rect; |
| 22 | import android.text.TextUtils; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.widget.TextView; |
| 25 | |
| 26 | import java.nio.charset.Charset; |
| 27 | import java.nio.charset.CharsetEncoder; |
| 28 | |
| 29 | /** |
| 30 | * Extended {@link TextView} that supports ascent/baseline alignment. |
| 31 | */ |
| 32 | public class AlignedTextView extends TextView { |
| 33 | |
| 34 | private static final String LATIN_CAPITAL_LETTER = "H"; |
| 35 | private static final CharsetEncoder LATIN_CHARSET_ENCODER = |
| 36 | Charset.forName("ISO-8859-1").newEncoder(); |
| 37 | |
| 38 | // temporary rect for use during layout |
| 39 | private final Rect mTempRect = new Rect(); |
| 40 | |
| 41 | private int mTopPaddingOffset; |
| 42 | private int mBottomPaddingOffset; |
| 43 | |
| 44 | public AlignedTextView(Context context) { |
| 45 | this(context, null /* attrs */); |
| 46 | } |
| 47 | |
| 48 | public AlignedTextView(Context context, AttributeSet attrs) { |
| 49 | this(context, attrs, android.R.attr.textViewStyle); |
| 50 | } |
| 51 | |
| 52 | public AlignedTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 53 | super(context, attrs, defStyleAttr); |
| 54 | |
| 55 | // Disable any included font padding by default. |
| 56 | setIncludeFontPadding(false); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 61 | CharSequence text = getText(); |
| 62 | if (TextUtils.isEmpty(text) || LATIN_CHARSET_ENCODER.canEncode(text)) { |
| 63 | // For latin text align to the default capital letter height. |
| 64 | text = LATIN_CAPITAL_LETTER; |
| 65 | } |
| 66 | getPaint().getTextBounds(text.toString(), 0, text.length(), mTempRect); |
| 67 | |
| 68 | final Paint textPaint = getPaint(); |
| 69 | mTopPaddingOffset = Math.min(getPaddingTop(), |
| 70 | (int) Math.floor(mTempRect.top - textPaint.ascent())); |
| 71 | mBottomPaddingOffset = Math.min(getPaddingBottom(), |
| 72 | (int) Math.floor(textPaint.descent())); |
| 73 | |
| 74 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public int getCompoundPaddingTop() { |
| 79 | return super.getCompoundPaddingTop() - mTopPaddingOffset; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int getCompoundPaddingBottom() { |
| 84 | return super.getCompoundPaddingBottom() - mBottomPaddingOffset; |
| 85 | } |
| 86 | } |