mobile/android/base/home/FadedTextView.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.home;
michael@0 7
michael@0 8 import android.content.Context;
michael@0 9 import android.content.res.TypedArray;
michael@0 10 import android.graphics.Canvas;
michael@0 11 import android.graphics.LinearGradient;
michael@0 12 import android.graphics.Shader;
michael@0 13 import android.graphics.drawable.Drawable;
michael@0 14 import android.util.AttributeSet;
michael@0 15 import android.widget.TextView;
michael@0 16
michael@0 17 import org.mozilla.gecko.R;
michael@0 18
michael@0 19 /**
michael@0 20 * FadedTextView fades the ends of the text by fadeWidth amount,
michael@0 21 * if the text is too long and requires an ellipsis.
michael@0 22 */
michael@0 23 public class FadedTextView extends TextView {
michael@0 24
michael@0 25 // Width of the fade effect from end of the view.
michael@0 26 private int mFadeWidth;
michael@0 27
michael@0 28 // Padding for compound drawables.
michael@0 29 private int mCompoundPadding;
michael@0 30
michael@0 31 public FadedTextView(Context context) {
michael@0 32 this(context, null);
michael@0 33 }
michael@0 34
michael@0 35 public FadedTextView(Context context, AttributeSet attrs) {
michael@0 36 this(context, attrs, android.R.attr.textViewStyle);
michael@0 37 }
michael@0 38
michael@0 39 public FadedTextView(Context context, AttributeSet attrs, int defStyle) {
michael@0 40 super(context, attrs, defStyle);
michael@0 41
michael@0 42 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FadedTextView);
michael@0 43 mFadeWidth = a.getDimensionPixelSize(R.styleable.FadedTextView_fadeWidth, 0);
michael@0 44 a.recycle();
michael@0 45
michael@0 46 mCompoundPadding = getCompoundDrawablePadding();
michael@0 47 }
michael@0 48
michael@0 49 /**
michael@0 50 * {@inheritDoc}
michael@0 51 */
michael@0 52 @Override
michael@0 53 public void onDraw(Canvas canvas) {
michael@0 54 int width = getMeasuredWidth();
michael@0 55
michael@0 56 // Layout doesn't return a proper width for getWidth().
michael@0 57 // Instead check the width of the first line, as we've restricted to just one line.
michael@0 58 if (getLayout().getLineWidth(0) > width) {
michael@0 59 final Drawable leftDrawable = getCompoundDrawables()[0];
michael@0 60 int drawableWidth = 0;
michael@0 61 if (leftDrawable != null) {
michael@0 62 drawableWidth = leftDrawable.getIntrinsicWidth() + mCompoundPadding;
michael@0 63 width -= drawableWidth;
michael@0 64 }
michael@0 65
michael@0 66 int color = getCurrentTextColor();
michael@0 67 float stop = ((float) (width - mFadeWidth) / (float) width);
michael@0 68 LinearGradient gradient = new LinearGradient(0, 0, width, 0,
michael@0 69 new int[] { color, color, 0x0 },
michael@0 70 new float[] { 0, stop, 1.0f - (drawableWidth / width) },
michael@0 71 Shader.TileMode.CLAMP);
michael@0 72 getPaint().setShader(gradient);
michael@0 73 } else {
michael@0 74 getPaint().setShader(null);
michael@0 75 }
michael@0 76
michael@0 77 // Do a default draw.
michael@0 78 super.onDraw(canvas);
michael@0 79 }
michael@0 80 }

mercurial